gpt4 book ai didi

java - isMultipartContent(请求)不起作用。我不知道发生了什么,它应该返回 true,但它返回 false

转载 作者:行者123 更新时间:2023-12-02 00:30:43 26 4
gpt4 key购买 nike

我正在使用此代码使用 jsp 在 Web 应用程序中上传文件(图像)。在此代码中 isMultipartContent(request) 应该返回 true,但它返回 false。我不明白出了什么问题。

<form class="form-contact contact_form" method="post" id="contactForm" novalidate="novalidate" enctype="multipart/form-data">
<div class="row">
<div class="col-sm-6">
<div class="form-group">
<input class="form-control" name="ff_ctype" id="ctype" type="text" onfocus="this.placeholder = ''" onblur="this.placeholder = 'Course Type'" placeholder = 'Course Type'>
</div>
</div>
<div class="col-12">
<div class="form-group">
<input name="cupload" id="cupload" type="file">
</div>
</div>
</div>
<div class="form-group mt-3">
<button type="submit" name="up_btn" value="submit" class="button button-contactForm btn_1">UPLOAD</button>
</div>
</form>
<%
String ctype="", cimage="", path="";
boolean successful=true;
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if(isMultipart) {
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List<FileItem> items = null;
try {
items = upload.parseRequest(request);
}
catch (FileUploadException e) {
e.printStackTrace();
}
for(FileItem myitem:items) {
if (myitem.isFormField()) {
String itemName1 = myitem.getFieldName();
String value=myitem.getString();
if(itemName1.equals("ff_ctype")) //control's name - textbox name
{
ctype=value;
}
}
else {
String type=myitem.getContentType();
long size=myitem.getSize()/1024; //kbytes
if(size==0) {
cimage="default.png";
}
else if((type.equals("image/pjpeg")
|| type.equals("image/jpeg")
|| type.equals("image/png")
|| type.equals("image/x-png")
|| type.equals("image/gif"))
&& size<400)
{
cimage=new java.util.Date().getTime()+myitem.getName();
path=config.getServletContext().getRealPath("/") + "uploads\\" + cimage;
File savefile=new File(path);
myitem.write(savefile);
}
else {
successful=false;
out.print("Sorry only pictures of less than 400kb are allowed to upload");
}
}
}
if(successful==true) {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection MyConnection=DriverManager.getConnection(PATH + PLACE, USERNAME, PASSWORD);
try {
String q1="insert into coursetypes(ctype, cimage) values(?,?)";
PreparedStatement insertvalues=MyConnection.prepareStatement(q1);
insertvalues.setString(1, ctype);
insertvalues.setString(2, cimage);
if(insertvalues.executeUpdate()==1) {
out.print("Image Uploaded Successfully");
}
else {
out.print("Error occured");
}
}
catch(Exception e) {
out.print("Error in query due to: " + e.getMessage());
}
}
catch(Exception e) {
out.print("Error in connection due to: "+e.getMessage());
}
}
}
}
%>

没有错误消息。

这段代码应该上传我项目中的 uploads 文件夹中的文件,但它的第一步出错了,即 isMultipartContent(request) 返回 false

最佳答案

我的错误是我使用了 form method='get'。但 multipart 不适用于 get 方法。因为据我所知,由于图像无法显示在地址栏上,所以这不起作用。这里显示的所有代码都可以完美运行。

错误的代码是:-

<form method="get">

正确的方法是:-

<form method="post">

此后,它工作得非常好。

关于java - isMultipartContent(请求)不起作用。我不知道发生了什么,它应该返回 true,但它返回 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58019471/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com