Java 文件上传“多部分/表单”异常 org.apache.commons.fileupload.FileUploadBase$InvalidContentTypeException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1458453/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 12:28:52  来源:igfitidea点击:

file upload "multipart/form" Exception org.apache.commons.fileupload.FileUploadBase$InvalidContentTypeException

javajspfile-uploadapache-commons

提问by Karthik.m

I tried to use the file upload using Apache Commons but the following exception thrown

我尝试使用 Apache Commons 上传文件,但抛出以下异常

org.apache.commons.fileupload.FileUploadBase$InvalidContentTypeException: the request doesn't contain a multipart/form-data or multipart/mixed stream, content type header is null

org.apache.commons.fileupload.FileUploadBase$InvalidContentTypeException:请求不包含多部分/表单数据或多部分/混合流,内容类型标头为空

My html code is

我的 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>

My JSP code is

我的 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);

   %>

采纳答案by skaffman

The form has to be method="POST"

表单必须是 method="POST"