views:

2709

answers:

2

I have simple web app that just has one Servlet that accepts data file and saves it to the server. I'm using "apache commons FileUpload" library. File uploading work fine on my local server (I'm using Glassfish for my Dev and Prod server). I can upload any size files. Here is my memory info:

-XX:MaxPermSize=512m
-Xmx1024m

Here is the code for my servlet:

...
public void doPost(HttpServletRequest request, HttpServletResponse response)  throws ServletException, IOException {
    log.info("Let's try to do something with your request");
    getMultipartRequestFile(request);
}

// Handling uploaded file with commons-fileupload library
private void getMultipartRequestFile(HttpServletRequest request){

    if(!ServletFileUpload.isMultipartContent(request))    // if not is multi part then exit this function
     return;

    FileItemFactory factory = new DiskFileItemFactory();  // Create a factory for file items
    ServletFileUpload upload = new ServletFileUpload(factory); // Create a new file upload handler

    //Create a progress listener
    ProgressListener progressListener = new ProgressListener(){
        private long megaBytes = -1;
           public void update(long pBytesRead, long pContentLength, int pItems) {
               long mBytes = pBytesRead / 1000000;
               if (megaBytes == mBytes) {
                   return;
               }
               megaBytes = mBytes;
               if (pContentLength == -1) {
                log.info("So far, " + dec.format(pBytesRead/1024.0/1024.0) + " have been read.");
               } else {
                log.info("So far, " + dec.format(pBytesRead/1024.0/1024.0) + "\tof " + dec.format(pContentLength/1024.0/1024.0));
               }
           }
    };
    upload.setProgressListener(progressListener);

    // Parse the request
    try {
     List items = upload.parseRequest(request);

     // Process the uploaded items
     Iterator iter = items.iterator();
     while (iter.hasNext()) {
         FileItem item = (FileItem) iter.next();

         if (item.isFormField()) {
             processFormField(item);
         } else {
             processUploadedFile(item);
         }
     }
    } 
    catch (FileUploadException e) {
     e.printStackTrace();
    }
}

// this is where all magic with the file will happen
private void processUploadedFile(FileItem item){

    if (!item.isFormField()) {

        String fieldName = item.getFieldName();
        String fileName = item.getName();
        String contentType = item.getContentType();
        boolean isInMemory = item.isInMemory();
        long sizeInBytes = item.getSize();

     log.info("Field name: " + fieldName + "\nFile Name: " + fileName + "\nContent type: " +contentType+ "\nSize: " + dec.format(sizeInBytes/1024.0/1024.0));

        // write uploaded file to hdd
        File uploadedFile = new File(FILES_FOLDER + fileName);  

        try {
      item.write(uploadedFile);   
      log.info("File location: " + FILES_FOLDER + fileName);

     } catch (Exception e) {
      log.warning("File cannot be writtet to the disc");
      e.printStackTrace();
     }
    }
}
...

And this is output with Error:

[#|2009-07-13T22:19:31.822+0000|INFO|sun-appserver2.1|com.athena.video.upload.server.Fileuploader|_ThreadID=18;_ThreadName=httpSSLWorkerThread-8080-0;|Let's try to do something with 
your request|#]

[#|2009-07-13T22:19:31.823+0000|INFO|sun-appserver2.1|com.athena.video.upload.server.Fileuploader|_ThreadID=18;_ThreadName=httpSSLWorkerThread-8080-0;|So far, 0.00 mb  of 87.64 mb|#]

[#|2009-07-13T22:19:33.403+0000|INFO|sun-appserver2.1|com.athena.video.upload.server.Fileuploader|_ThreadID=18;_ThreadName=httpSSLWorkerThread-8080-0;|So far, 0.95 mb  of 87.64 mb|#]

[#|2009-07-13T22:19:34.109+0000|INFO|sun-appserver2.1|com.athena.video.upload.server.Fileuploader|_ThreadID=18;_ThreadName=httpSSLWorkerThread-8080-0;|So far, 1.91 mb  of 87.64 mb|#]

[#|2009-07-13T22:19:34.739+0000|INFO|sun-appserver2.1|com.athena.video.upload.server.Fileuploader|_ThreadID=18;_ThreadName=httpSSLWorkerThread-8080-0;|So far, 2.86 mb  of 87.64 mb|#]

[#|2009-07-13T22:19:35.371+0000|INFO|sun-appserver2.1|com.athena.video.upload.server.Fileuploader|_ThreadID=18;_ThreadName=httpSSLWorkerThread-8080-0;|So far, 3.82 mb  of 87.64 mb|#]

[#|2009-07-13T22:19:35.989+0000|INFO|sun-appserver2.1|com.athena.video.upload.server.Fileuploader|_ThreadID=18;_ThreadName=httpSSLWorkerThread-8080-0;|So far, 4.77 mb  of 87.64 mb|#]

[#|2009-07-13T22:19:36.938+0000|WARNING|sun-appserver2.1|javax.enterprise.system.stream.err|_ThreadID=18;_ThreadName=httpSSLWorkerThread-8080-0;_RequestID=99b72ec5-047c-4a86-9160-994ea31848a2;|org.apache.commons.fileupload.FileUploadBase$IOFileUploadException: Processing of multipart/form-data request failed. Stream ended unexpectedly
        at org.apache.commons.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:367)
        at org.apache.commons.fileupload.servlet.ServletFileUpload.parseRequest(ServletFileUpload.java:126)
        at com.athena.video.upload.server.Fileuploader.getMultipartRequestFile(Fileuploader.java:109)
        at com.athena.video.upload.server.Fileuploader.doPost(Fileuploader.java:47)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:754)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
        at org.apache.catalina.core.ApplicationFilterChain.servletService(ApplicationFilterChain.java:427)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:315)
        at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:287)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:218)
        at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:648)
        at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:593)
        at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:94)
        at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:98)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:222)
        at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:648)
        at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:593)
        at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:587)
        at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:1096)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:166)
        at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:648)
        at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:593)
        at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:587)
        at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:1096)
        at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:288)
        at com.sun.enterprise.web.connector.grizzly.DefaultProcessorTask.invokeAdapter(DefaultProcessorTask.java:647)
        at com.sun.enterprise.web.connector.grizzly.DefaultProcessorTask.doProcess(DefaultProcessorTask.java:579)
        at com.sun.enterprise.web.connector.grizzly.DefaultProcessorTask.process(DefaultProcessorTask.java:831)
        at com.sun.enterprise.web.connector.grizzly.DefaultReadTask.executeProcessorTask(DefaultReadTask.java:341)
        at com.sun.enterprise.web.connector.grizzly.DefaultReadTask.doTask(DefaultReadTask.java:263)
        at com.sun.enterprise.web.connector.grizzly.DefaultReadTask.doTask(DefaultReadTask.java:214)
        at com.sun.enterprise.web.portunif.PortUnificationPipeline$PUTask.doTask(PortUnificationPipeline.java:380)
        at com.sun.enterprise.web.connector.grizzly.TaskBase.run(TaskBase.java:265)
        at com.sun.enterprise.web.connector.grizzly.ssl.SSLWorkerThread.run(SSLWorkerThread.java:106)
Caused by: org.apache.commons.fileupload.MultipartStream$MalformedStreamException: Stream ended unexpectedly
        at org.apache.commons.fileupload.MultipartStream$ItemInputStream.makeAvailable(MultipartStream.java:983)
        at org.apache.commons.fileupload.MultipartStream$ItemInputStream.read(MultipartStream.java:887)
        at java.io.InputStream.read(InputStream.java:85)
        at org.apache.commons.fileupload.util.Streams.copy(Streams.java:94)
        at org.apache.commons.fileupload.util.Streams.copy(Streams.java:64)
        at org.apache.commons.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:362)
        ... 33 more
|#]

[#|2009-07-13T22:19:37.839+0000|SEVERE|sun-appserver2.1|javax.enterprise.system.container.web|_ThreadID=19;_ThreadName=httpSSLWorkerThread-8080-1;_RequestID=528b60b2-7083-4f27-a1de-0c90df2a34f1;|WEB0777: Unblocking keep-alive exception
java.lang.IllegalStateException: PWC4662: Request header is too large
        at org.apache.coyote.http11.InternalInputBuffer.fill(InternalInputBuffer.java:740)
        at org.apache.coyote.http11.InternalInputBuffer.parseRequestLine(InternalInputBuffer.java:442)
        at com.sun.enterprise.web.connector.grizzly.DefaultProcessorTask.parseRequest(DefaultProcessorTask.java:694)
        at com.sun.enterprise.web.connector.grizzly.DefaultProcessorTask.doProcess(DefaultProcessorTask.java:577)
        at com.sun.enterprise.web.connector.grizzly.DefaultProcessorTask.process(DefaultProcessorTask.java:831)
        at com.sun.enterprise.web.connector.grizzly.DefaultReadTask.executeProcessorTask(DefaultReadTask.java:341)
        at com.sun.enterprise.web.connector.grizzly.DefaultReadTask.doTask(DefaultReadTask.java:263)
        at com.sun.enterprise.web.connector.grizzly.DefaultReadTask.doTask(DefaultReadTask.java:214)
        at com.sun.enterprise.web.connector.grizzly.TaskBase.run(TaskBase.java:265)

Here is what I also tried to add to my domain.xml:

<http-listener acceptor-threads="1" address="0.0.0.0" blocking-enabled="false" default-virtual-server="server" enabled="true" family="inet" id="http-listener-1" port="8080" security-enabled="false" server-name="" xpowered-by="true">
          <property name="maxPostSize" value="0"/> <!-- 0 means no max -->
          <property name="proxiedProtocols" value="ws/tcp"/>
</http-listener>

Any idea why this is happening

+1  A: 
java.lang.IllegalStateException: PWC4662: Request header is too large

You need to configure the HTTP connector to use a larger buffer.

There should be a parameter "maxPostSize" in the settings of the HTTP listener.

Thilo
Nope, did not work. Here is what I have in my domain.xml file:<http-listener acceptor-threads="1" address="0.0.0.0" blocking-enabled="false" default-virtual-server="server" enabled="true" family="inet" id="http-listener-1" port="8080" security-enabled="false" server-name="" xpowered-by="true"> <property name="maxPostSize" value="0"/> <!-- 0 means no max --> <property name="proxiedProtocols" value="ws/tcp"/> </http-listener>
Maksim
+1  A: 

I can only speculate. Do you use the same version of the library/container on both servers? Could you sniff your file upload (using an apache tool - can't remember the name) and check if it gets uploaded properly? You could manually grab the uploaded body in the servlet and check if there is no corruption from a servlet filter/default request parser?

I also found a this issue here. Basically it states that socket timeout during the upload caused the exception.

kd304