java – 文件上传“multipart / form”异常org.apache.commons.fileupload.FileUploadBase $InvalidContentTypeException

前端之家收集整理的这篇文章主要介绍了java – 文件上传“multipart / form”异常org.apache.commons.fileupload.FileUploadBase $InvalidContentTypeException前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我尝试使用Apache Commons上传文件,但抛出了以下异常

org.apache.commons.fileupload.FileUploadBase $InvalidContentTypeException:请求不包含multipart / form-data或multipart / mixed流,内容类型标头为null

我的HTML代码

<form  name="inp" action="upload.jsp"  method="get" onsubmit="return valid();" enctype="multipart/form-data">
<table align="center" cellspacing="2">

  <tr><td><font size="5" color="#E41B17">Select File</font> </td>
<td><input type="file" name="infile"></td>
</tr>
<tr><td><font size="5" color="#E41B17">Target File Name</font></td>
<td><input type="text" size="20" name="filename"></input></td>
</tr>
<tr></tr>
<tr><td colspan="2" align="center"><input type=submit value="Upload"  ></td></tr>
</table>
<br></br>
<center>
<a href="index.html"><font color="#E41B17">HOME</font></a>
</center>
</form>

我的JSP代码

<% 
   String user = (String)session.getAttribute("uname");
   String f = request.getParameter("filename");

   DiskFileUpload upload = new DiskFileUpload();         
   boolean isMultipart=upload.isMultipartContent(request);


   upload.setSizeMax(1048576);     
   List items = upload.parseRequest(request);  
   FileItem  file = (FileItem) items.get(0); 

   String source = file.getName();
      String delim="\\";
   String str="";
   File propfile=new File("C:\\eclipse_practise\\fileupload\\WebContent\\path.properties");

   BufferedInputStream propbuf=new BufferedInputStream(new FileInputStream(propfile));

   Properties path=new Properties();

   path.load(propbuf);

   String serverlocation=path.getProperty("Server_path");

   session.setAttribute("storelocation",serverlocation);

   StringTokenizer st = new StringTokenizer(source,delim);

   while(st.hasMoreTokens())
   {                        
       str=st.nextToken();
   }

   FileItem  name = (FileItem) items.get(1);

   String  target = name.getString();

   File outfile = new File(serverlocation+target);

    file.write(outfile); 

      session.setAttribute("filename",target);

   %>

解决方法

表单必须是method =“POST”
原文链接:https://www.f2er.com/java/130162.html

猜你在找的Java相关文章