Skip to main content

Large File Upload giving Exception

6 replies [Last post]
babbarkrishan
Offline
Joined: 2007-01-18
Points: 0

Hi,
I am using GlassFish 3. I am getting following exception while uploading large files.
[#|2011-02-14T19:58:23.781+0530|SEVERE|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=24;_ThreadName=Thread-1;|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 net.paxcel.fileupload.FileUploadLoadTest.doPost(FileUploadLoadTest.java:65)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:754)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1523)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:279)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:188)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:641)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:97)
at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:85)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:185)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:325)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:226)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:165)
at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:791)
at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:693)
at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:954)
at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:170)
at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:135)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:102)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:88)
at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:76)
at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:53)
at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:57)
at com.sun.grizzly.ContextTask.run(ContextTask.java:69)
at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:330)
at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:309)
at java.lang.Thread.run(Unknown Source)
Caused by: org.apache.commons.fileupload.MultipartStream$MalformedStreamException: Stream ended unexpectedly

It is working fine with small files. My server side code is given below:

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

public class FileUploadLoadTest extends HttpServlet {

private static final long serialVersionUID = 5277879617123968993L;
private static final String TMP_DIR_PATH = Configuration.getConfig().getValue("FILE_UPLOAD_TMP_DIR");
private File tmpDir;
private static final String DESTINATION_DIR_PATH = Configuration.getConfig().getValue("DESTINATION_DIR_PATH");
private File destinationDir;

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("In doPost ");
DiskFileItemFactory fileItemFactory = new DiskFileItemFactory ();
/*
*Set the size threshold, above which content will be stored on disk.
*/
System.out.println("Temp Dir " + TMP_DIR_PATH);

/*
* Set the temporary directory to store the uploaded files of size above threshold.
*/
tmpDir = new File(TMP_DIR_PATH);
fileItemFactory.setRepository(tmpDir);
if(!tmpDir.isDirectory()) {
System.out.println(TMP_DIR_PATH + " is not a directory");
throw new ServletException(TMP_DIR_PATH + " is not a directory");
}
String realPath = DESTINATION_DIR_PATH;//getServletContext().getRealPath(DESTINATION_DIR_PATH);
System.out.println(" realPath: " + realPath);
destinationDir = new File(realPath);
if(!destinationDir.isDirectory()) {
System.out.println(DESTINATION_DIR_PATH+ " is not a directory");
throw new ServletException(DESTINATION_DIR_PATH+" is not a directory");
}

ServletFileUpload uploadHandler = new ServletFileUpload(fileItemFactory);

response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<body bgcolor=FFFFFF>");

try {
/*
* Parse the request
*/
List items = uploadHandler.parseRequest(request);
System.out.println("uploadHandler.parseRequest");
Iterator itr = items.iterator();
while(itr.hasNext()) {
System.out.println("itr.hasNext()");
FileItem item = (FileItem) itr.next();
/*
* Handle Form Fields.
*/
if(item.isFormField()) {
System.out.println("File Name = "+item.getFieldName()+", Value = "+item.getString());
} else {
//Handle Uploaded files.
System.out.println("Field Name = "+item.getFieldName()+
", File Name = "+item.getName()+
", Content type = "+item.getContentType()+
", File Size = "+item.getSize());
/*
* Write file to the ultimate location.
*/
File file = new File(destinationDir, System.currentTimeMillis() + "_" + item.getName());
item.write(file);
}
}
out.println("<h2>File uploaded successfully.</h2>");
} catch(FileUploadException ex) {
ex.printStackTrace();
System.out.println("Error encountered while parsing the request");

} catch(Exception ex) {
ex.printStackTrace();
System.out.println("Error encountered while uploading file");

}

out.println("<h2>Done...</h2></body>");
out.close();

System.out.println("Exit........" );
}
}

Please help me to resolve this issue.

Thanks
Krishan Babbar

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
ucrkarthik
Offline
Joined: 2010-09-18
Points: 0

Did you ever find a solution?

Thanks,
Karthik

oleksiys
Offline
Joined: 2006-01-25
Points: 0

Hi,

do you see any warning or exception in the server.log when it happens?

Thanks.

WBR,
Alexey.

ucrkarthik
Offline
Joined: 2010-09-18
Points: 0

Here is the exception that I receive:

[#|2012-10-19T13:08:53.502-0700|WARNING|oracle-glassfish3.1.2|javax.enterprise.system.container.web.com.sun.enterprise.web|_ThreadID=22;_ThreadName=Thread-2;|StandardWrapperValve[applicationContext-mvc]: PWC1406: Servlet.service() for servlet applicationContext-mvc threw exception
org.apache.commons.fileupload.MultipartStream$MalformedStreamException: Stream ended unexpectedly
at org.apache.commons.fileupload.MultipartStream$ItemInputStream.makeAvailable(MultipartStream.java:982)
at org.apache.commons.fileupload.MultipartStream$ItemInputStream.read(MultipartStream.java:886)
at java.io.InputStream.read(InputStream.java:82)
at org.apache.commons.fileupload.util.Streams.copy(Streams.java:96)
at org.apache.commons.fileupload.util.Streams.copy(Streams.java:66)
at org.apache.commons.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:366)
at org.apache.commons.fileupload.servlet.ServletFileUpload.parseRequest(ServletFileUpload.java:126)
at org.springframework.web.multipart.commons.CommonsMultipartResolver.parseRequest(CommonsMultipartResolver.java:156)
at org.springframework.web.multipart.commons.CommonsMultipartResolver.resolveMultipart(CommonsMultipartResolver.java:139)
at org.springframework.web.servlet.DispatcherServlet.checkMultipart(DispatcherServlet.java:1020)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:883)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:688)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:770)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1550)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:860)
at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:757)
at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1056)
at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:229)
at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
at java.lang.Thread.run(Thread.java:662)
|#]

Thanks,
Karthik

oleksiys
Offline
Joined: 2006-01-25
Points: 0

Hi,

do you see this problem happending all the time, or sporadically (when server is overloaded for example, or client connection is very slow)?

Thanks.

WBR,
Alexey.

ucrkarthik
Offline
Joined: 2010-09-18
Points: 0

I am running this locally and I see the errors in the following situations:
- after I deploy the artifact and the 1st time I upload a file.
- when the page is ideal for a long time.

oleksiys
Offline
Joined: 2006-01-25
Points: 0

Can you pls. attach the testcase for us, so we can reproduce the issue?
Thanks.