8

I upload the file which I browse with input type="file" in my web App. The issue is I get the FileItem list size as 0 though I can see all uploaded file info under

request -> JakartaMutltiPartRequest -> files attribute

Here is 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 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>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
M Sach
  • 33,416
  • 76
  • 221
  • 314

4 Answers4

14

As I said in a comment to the same question, you posted earlier, this is most likely because you have parsed the request already before. The files are part of the request body and you can parse it only one time.

Update:

I usually do use commons-upload in that 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();
        }
    }
}
slim
  • 2,545
  • 1
  • 24
  • 38
Udo Klimaschewski
  • 5,150
  • 1
  • 28
  • 41
3

You should check for multipart 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) {
Roman C
  • 49,761
  • 33
  • 66
  • 176
  • isMultipart content evalues to true – M Sach Oct 24 '12 at 12:47
  • @MSach it seems that action url have collisions with struts filter. If you are using struts why don't you use its fileupload feature? Post web.xml and remove everything except servlet configuration. – Roman C Oct 24 '12 at 16:51
0

How big are the files you are uploading? You might be over the default threshold. I think the default is 10K

factory.setSizeThreshold(maxSizeYouWantToHandle);
mprivat
  • 21,582
  • 4
  • 54
  • 64
  • The threshold sets the limit up what size the data will be kept in memory, if the threshold (10K default is correct) is passed, the data will be stored on disk. In java.io.tempdir by default. – Udo Klimaschewski Oct 24 '12 at 12:26
0

If you are using weblogic 12 then check whether the patch_wls1211 is insatlled or not. I was having the same issue and it resolved after applying patch_wls1211.

Prasanth
  • 35
  • 4