12

I would like to upload a file in my JSF application. I am using a Filter and HttpServletRequestWrapper to access the upload file.

 public MultipartRequestWrapper(HttpServletRequest request) {
    super(request);
    System.out.println("Created multipart wrapper....");
    try {
        System.out.println("Looping parts"+getParts().size());

        for (Part p : getParts()) {
            System.out.println(String.format("Part name: %1$s, contentType : %2$s", p.getName(), p.getContentType()));
            for(String header : p.getHeaderNames()){
                System.out.println("Header name : " + header + ", value : " + p.getHeader(header));
            }
            byte[] b = new byte[(int) p.getSize()];
            p.getInputStream().read(b);
            params.put(p.getName(), new String[]{new String(b)});
        }
    } catch (IOException ex) {
        ex.printStackTrace();
        Logger.getLogger(MultipartRequestWrapper.class.getName()).log(Level.SEVERE, null, ex);
    } catch (ServletException ex) {
         ex.printStackTrace();
        Logger.getLogger(MultipartRequestWrapper.class.getName()).log(Level.SEVERE, null, ex);
    }

However, getParts() returns an empty collection. How can I enable multipart/form-data parsing in a servlet filter in Tomcat 7.0.8?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Niroshan Abayakoon
  • 913
  • 1
  • 10
  • 22

2 Answers2

32

In order to get HttpServletRequest#getParts() to work in a Filter in Tomcat, you need to set allowCasualMultipartParsing="true" in the webapp's <Context> element in Webapp/META-INF/context.xml or Tomcat/conf/server.xml.

<Context ... allowCasualMultipartParsing="true">

Because as per the servlet 3.0 specification the HttpServletRequest#getParts() should only be available inside a HttpServlet with the @MultipartConfig annotation. See also the documentation of the <Context> element:

allowCasualMultipartParsing

Set to true if Tomcat should automatically parse multipart/form-data request bodies when HttpServletRequest.getPart* or HttpServletRequest.getParameter* is called, even when the target servlet isn't marked with the @MultipartConfig annotation (See Servlet Specification 3.0, Section 3.2 for details). Note that any setting other than false causes Tomcat to behave in a way that is not technically spec-compliant. The default is false.

See also:


Unrelated to the concrete problem, the following is definitely not right:

byte[] b = new byte[(int) p.getSize()];
p.getInputStream().read(b);
params.put(p.getName(), new String[]{new String(b)});

First, you are not respecting the character encoding specified by the client -if any. Second, this will fail for binary files.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • I am using tomcat 7.0.33 I am using apache commons fileupload and I make change in Tomcat/conf/context.xml as above but it not work for me. Please help – Mayank Pandya Jan 22 '13 at 20:32
  • @Mayank: This question/answer is not about Apache Commons FileUpload. It's about the new Servlet 3.0 builtin `multipart/form-data` parser. This Tomcat configuration setting has completely nothing to do with Apache Commons FileUpload. Your concrete problem is caused elsewhere. Just press `Ask Question` if you still stucks. In the meanwhile, this answer may be helpful: http://stackoverflow.com/questions/2422468/how-to-upload-files-to-server-using-jsp-servlet/2424824#2424824 – BalusC Jan 22 '13 at 20:40
  • thank you for your quick reply but I am little bit confuse about my issue because multipart request cancelled automatically by tomcat. not even servelt init called. Any suggestions please? – Mayank Pandya Jan 22 '13 at 20:55
  • There is a similar solution to enable upload by filter under Wildfly? – Otávio Garcia Aug 10 '14 at 07:24
0

Also try Multipart filter. worked for me.

<filter>
    <filter-name>MultipartFilter</filter-name>
    <filter-class>org.eclipse.jetty.servlets.MultiPartFilter</filter-class>
</filter> 
<filter-mapping>
    <filter-name>MultipartFilter</filter-name>
    <servlet-name>CamelServlet</servlet-name> 
</filter-mapping>
Kukeltje
  • 12,223
  • 4
  • 24
  • 47
navaltiger
  • 884
  • 12
  • 27