【问题标题】:file upload "multipart/form" Exception org.apache.commons.fileupload.FileUploadBase$InvalidContentTypeException文件上传“multipart/form”异常 org.apache.commons.fileupload.FileUploadBase$InvalidContentTypeException
【发布时间】:2010-11-30 07:35:18
【问题描述】:

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

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

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

   %>

【问题讨论】:

    标签: java jsp file-upload apache-commons


    【解决方案1】:

    表单必须是method="POST"

    【讨论】:

    • 请说明为什么该方法必须“POST”
    • 因为当你点击提交按钮时,表单也会收集所有数据,包括文件(图片)。并打包并以http请求发送到服务器..
    • 在这种情况下,你声明 method="get" ,数据在超链接中可见..即url?param1=value$param2=value..并且这个http请求有长度限制。所以在请求 travll 时丢失了一些数据。所以我们使用 method="post" 在这篇文章中,表单收集所有信息并将其作为一个捆绑包发送到服务器....所以最后表单必须声明为“post”类型,同时处理任何媒体文件...
    猜你喜欢
    • 1970-01-01
    • 2015-10-23
    • 1970-01-01
    • 1970-01-01
    • 2020-01-15
    • 2016-01-27
    • 2011-02-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多