tags:

views:

592

answers:

1

In attempting to implement fileUpload using the streaming API I get an error described below: As I gather from the Streaming API web page that the "traditional FileItemFactory is completely ignored," I'm wondering what I'm doing wrong. So I've defined a basic method as follows:

/**
 * 
 * @param
 * @return
 */
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {

    // Check that we have a file upload request
    boolean isMultipart = ServletFileUpload.isMultipartContent(request);

    if (isMultipart) {

        ServletFileUpload upload = new ServletFileUpload();                 // Create a new file upload handler

            // Parse the request
            try {
Line 39 -->         List items = upload.parseRequest(request);                      // FileItem
            Iterator iter = items.iterator();
            while (iter.hasNext()) {
                FileItemStream item = (FileItemStream) iter.next();

                if (!item.isFormField()) {
                    this.processFITSFile(item);
                }
            }
        } catch (FileUploadException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }       
    }
}

But that leads to the error below:

SEVERE: Servlet.service() for servlet FitsFileProcessorServlet threw exception
java.lang.NullPointerException: No FileItemFactory has been set.
    at org.apache.commons.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:353)
    at org.apache.commons.fileupload.servlet.ServletFileUpload.parseRequest(ServletFileUpload.java:126)
    at controller.FITSFileProcessor.doPost(FITSFileProcessor.java:39)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454)
    at java.lang.Thread.run(Unknown Source)
+2  A: 

new ServletFileUpload() creates an uninitialized instance. Its documentation says:

Constructs an uninitialised instance of this class. A factory must be configured, using setFileItemFactory(), before attempting to parse requests.

So you either need to use setFileItemFactory() or use the other constructor, which takes the factory as an argument. Like this:

// Create a factory for disk-based file items
FileItemFactory factory = new DiskFileItemFactory();

// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);

Fore more info, see the documentation. The example is also from there.

Or, you can use the streaming API, but then you need to get the iterator differently:

ServletFileUpload upload = new ServletFileUpload();
FileItemIterator iter = upload.getItemIterator(request);
while (iter.hasNext()) {
    FileItemStream item = iter.next();
    ...
}
Thomas Lötzer
but then doing so yields:java.lang.ClassCastException: org.apache.commons.fileupload.disk.DiskFileItem cannot be cast to org.apache.commons.fileupload.FileItemStream controller.FITSFileProcessor.doPost(FITSFileProcessor.java:46) javax.servlet.http.HttpServlet.service(HttpServlet.java:637) javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
Yaw Reuben
You need to do `upload.getItemIterator(request)` if you want to use the streaming API, see http://commons.apache.org/fileupload/streaming.html
Thomas Lötzer
@Yaw ReubenI updated my answer to include the streaming API
Thomas Lötzer