Uploading a file using ServletFileUpload parseRequest?

I am uploading a file that I am viewing with input type="file" in my web application. The problem is that I get the size of the FileItem list as 0, although I can see all the information about the downloaded file in the section

request JakartaMutltiPartRequest file attribute

Here is the Java code that reads the file

 public InputStream parseRequestStreamWithApache(HttpServletRequest request) throws FileUploadException, IOException { InputStream is = null; FileItemFactory factory = new DiskFileItemFactory(); ServletFileUpload upload = new ServletFileUpload(factory); List items = upload.parseRequest(request); // here the item size is 0 ,i am not sure why i am not getting my file upload in browser with type="file" // but If inspect request in debugger i can see my file realted info in request--->JakartaMutltiPartRequest----->files attribute Iterator iter = items.iterator(); while (iter.hasNext()) { FileItem item = (FileItem) iter.next(); if (!item.isFormField()) { is = item.getInputStream(); } } return is; } 

EDIT:

Here is the JSP part:

 <form NAME="form1" action="customer/customerManager!parseRequestStreamWithApache.action" ENCTYPE="multipart/form-data" method="post" > <TABLE > <tr> <th>Upload File</th> <td> <input name="fileUploadAttr" id="filePath" type="file" value=""> </td> <td > <Input type="submit" value ="uploadFile"/> </td> </tr> </TABLE> </form> 
+6
source share
4 answers

As I said in a comment on the same question posted earlier, this is most likely because you already parsed the request. Files are part of the request body, and you can analyze it only once.

Update:

I usually use commons-upload this way:

 if (ServletFileUpload.isMultipartContent(request)) { ServletFileUpload fileUpload = new ServletFileUpload(); FileItemIterator items = fileUpload.getItemIterator(request); // iterate items while (items.hasNext()) { FileItemStream item = items.next(); if (!item.isFormField()) { is = item.openStream(); } } } 
+11
source

You must check out the multi-page content.

 boolean isMultipart = ServletFileUpload.isMultipartContent(request); if (isMultipart) { FileItemFactory factory = new DiskFileItemFactory(); ServletFileUpload upload = new ServletFileUpload(factory); try { List items = upload.parseRequest(request); Iterator iterator = items.iterator(); while (iterator.hasNext()) { FileItem item = (FileItem) iterator.next(); if (!item.isFormField()) { String fileName = item.getName(); String root = getServletContext().getRealPath("/"); File path = new File(root + "/fileuploads"); if (!path.exists()) { boolean status = path.mkdirs(); } File uploadedFile = new File(path + "/" + fileName); item.write(uploadedFile); } } } catch (Exception e) { 
+3
source

How big are the files you download? You may have exceeded the default threshold. I think the default is 10K

 factory.setSizeThreshold(maxSizeYouWantToHandle); 
0
source

If you are using weblogic 12, check if patch_wls1211 is damaged or not. I had the same problem and it resolved after applying patch_wls1211.

0
source

All Articles