文件上载“multipart / form”异常org.apache.commons.fileupload.FileUploadBase $ InvalidContentTypeException [英] file upload "multipart/form" Exception org.apache.commons.fileupload.FileUploadBase$InvalidContentTypeException

查看:629
本文介绍了文件上载“multipart / form”异常org.apache.commons.fileupload.FileUploadBase $ InvalidContentTypeException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用Apache Commons上传文件,但抛出了以下异常:
$ b org.apache.commons.fileupload.FileUploadBase $ InvalidContentTypeException:请求不包含一个multipart / form-data或multipart / mixed流,内容类型标头为空

我的html代码是

 < form name =inpaction =upload.jspmethod =getonsubmit =return valid(); ENCTYPE = 多部分/格式数据 > 
< table align =centercellspacing =2>

< tr>< td>< font size =5color =#E41B17>选择文件< / font> < / TD>
< td>< input type =filename =infile>< / td>
< / tr>
< tr>< td>< font size =5color =#E41B17>目标档案名称< / font>< / td>
< td>< input type =textsize =20name =filename>< / input>< / td>
< / tr>
< tr>< / tr>
< tr>< td colspan =2align =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 =;
文件propfile =新文件(C:\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\');

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

属性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();

文件outfile =新文件(serverlocation + target);

file.write(outfile);

session.setAttribute(filename,target);

%>


解决方案

表单必须是method =POST / p>

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

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

My html code is

<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

   <% 
   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);

   %>

解决方案

The form has to be method="POST"

这篇关于文件上载“multipart / form”异常org.apache.commons.fileupload.FileUploadBase $ InvalidContentTypeException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆