File upload with ServletFileUpload's parseRequest? [duplicate]
Asked Answered
S

4

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>
Seclusion answered 24/10, 2012 at 12:9 Comment(1)
Uploading to struts 2 action?Ashien
G
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();
        }
    }
}
Gladi answered 24/10, 2012 at 12:16 Comment(5)
But i did not the parse the request earlierSeclusion
Can you show the rest of your servlet code that does anything with your HttpServletRequest before parseRequestStreamWithApache() is called?Gladi
I placed the code in very first filter of my webapp, still the same result.So looks like that is not the issueSeclusion
I updated my answer with some code that does it a bit different, maybe you can give it a try? Allways worked for me.Gladi
Thanks Udo. You was right. looks like struts2 fileupload plugin is already reading in between.Seclusion
A
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) {

Example of such servlet you can find here.

Ashien answered 24/10, 2012 at 12:23 Comment(2)
isMultipart content evalues to trueSeclusion
@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.Ashien
S
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);
Sorensen answered 24/10, 2012 at 12:18 Comment(1)
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.Gladi
C
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.

Centrosymmetric answered 14/11, 2014 at 18:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.