jsp中的文件上传问题

时间:2011-12-07 08:47:11

标签: jsp file-upload multipartform-data

我必须上传一个文件,而iam尝试运行显示消息

 org.apache.jasper.JasperException: Exception in JSP: /RootSipResource.jsp:33

30:        out.println("Cookies" + request.getCookies());
31:        
32:        DiskFileUpload upload=new DiskFileUpload();
33:        List items=upload.parseRequest(request);
34:         Iterator it=items.iterator();   
35:        while(it.hasNext())  
36:                {          


Stacktrace:
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:504)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:375)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
    org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:368)

root cause

javax.servlet.ServletException: the request doesn't contain a multipart/form-data or multipart/mixed stream, content type header is null
    org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:858)
    org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:791)
    org.apache.jsp.RootSipResource_jsp._jspService(RootSipResource_jsp.java:577)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:332)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
    org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:368)

root cause

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$FileItemIteratorImpl.<init>(FileUploadBase.java:768)
    org.apache.commons.fileupload.FileUploadBase.getItemIterator(FileUploadBase.java:323)
    org.apache.commons.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:341)
    org.apache.commons.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:302)
    org.apache.jsp.RootSipResource_jsp._jspService(RootSipResource_jsp.java:91)

在jsp页面中,表单字段设置为

<form name="frm" id="frmLoad" method="post" enctype="multipart/form-data">  

isMultipart打印为true(boolean isMultipart = FileUpload.isMultipartContent(request);)

       `out.println("Content Type ="+request.getContentType());` is printed as null

1 个答案:

答案 0 :(得分:6)

此异常表示method="post"元素中缺少enctype="multipart/form-data"<form>。但由于它们显然存在,其原因在于其他地方。也许你正在嵌套表单,或者误解结果,或者没有运行你认为正在运行的代码。

无论如何,应该避免在JSP文件中编写Java代码。使用servlet。您可以在此答案中找到具体示例:How to upload files to server using JSP/Servlet?