Java isMultipartContent(请求)不工作。我不知道发生了什么,它应该是真的,但是它是假的
我使用这段代码在使用jsp的web应用程序中上载文件(图像)。在这段代码中,isMultipartContent(请求)应该返回true,但它返回false。我不明白什么是错的Java isMultipartContent(请求)不工作。我不知道发生了什么,它应该是真的,但是它是假的,java,jsp,Java,Jsp,我使用这段代码在使用jsp的web应用程序中上载文件(图像)。在这段代码中,isMultipartContent(请求)应该返回true,但它返回false。我不明白什么是错的 <form class="form-contact contact_form" method="post" id="contactForm" novalidate="novalidate" enctype="multipart/form-data"> <div c
<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());
}
}
}
}
%>
上传
我的错误是我使用了formmethod='get'。但multipart不适用于get方法。因为,据我所知,由于图像不能显示在地址栏上,所以这不起作用。这里显示的所有代码都工作正常
错误代码是:-
<form method="get">
正确的方法是:-
<form method="post">
在这之后,它工作得非常好