Skip to main content

Large File Upload giving Exception

1 reply [Last post]
babbarkrishan
Offline
Joined: 2007-01-18

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

Any luck? I am also getting this error.