4

I have a multipart/form-data form with some <input type='text'> and <input type='file'> fields.

I use this code

 List<FileItem> multipartItems = null;
 boolean isMultipart = ServletFileUpload.isMultipartContent(request);
 if (!isMultipart) {
   System.out.println("false");
 } else {
   FileItemFactory factory = new DiskFileItemFactory();
   ServletFileUpload upload = new ServletFileUpload(factory);
   multipartItems = null;
       try {
           multipartItems = upload.parseRequest(request);
           System.out.println("true "+multipartItems.toString());

       } catch (FileUploadException e) {
       e.printStackTrace();
       }

      }

to find out if the form has multipart content. Then, I use

   Map<String, String[]> parameterMap = new HashMap<String, String[]>();

     for (FileItem multipartItem : multipartItems) {
        if (multipartItem.isFormField()) {
            processFormField(multipartItem, parameterMap);
        } else {
            request.setAttribute(multipartItem.getFieldName(), multipartItem);
        }
     }

By running the first snippet of code, the else is executed, but at the end multipartItems is null.

For this reason, the for in the second code snippet is never executed.

I don't know why there is this behaviour. I'm using Struts 1.3.10

EDIT

How can I check if struts has already parsed the request?

If so, is there a way to turn off it only for a particular form?

EDIT 2

I have a dynamic form, coded in json format. I have a form bean for json and for hidden properties. Then I parse the json "by hand". All works perfectly, but now I have to add input type=file fields and use the multipart/form-data enctype.

To prevent struts request parsing I put in the web.xml:

<init-param>
    <param-name>multipartClass</param-name>
    <param-value>none</param-value>
</init-param>

But it doesn't seem to work

2
  • 2
    Why are you doing any of this if you're using Struts, where this is handled automatically? If it's in a servlet, why tag the question with Struts? Commented Sep 12, 2013 at 12:40
  • @DaveNewton: I have a dynamic form, coded in json format. I have a form bean for json and for hidden properties. Then I parse the json "by hand". All works perfectly, but now I have to add input type=file fields and use the multipart/form-data enctype. Commented Sep 12, 2013 at 12:44

2 Answers 2

2
+25

Initialize a FileItem, like below:

     FileItem fileItem = null;

Then call this method

    public boolean getParameters(HttpServletRequest request, PrintWriter out) {
    List fileItemsList = null;
    try {
        if (ServletFileUpload.isMultipartContent(request)) {
            ServletFileUpload servletFileUpload = new ServletFileUpload(new DiskFileItemFactory());
            try {
                fileItemsList = servletFileUpload.parseRequest(request);
            } catch (FileUploadException ex) {
            }
            String optionalFileName = "";
            Iterator it = fileItemsList.iterator();
            while (it.hasNext()) {
                FileItem fileItemTemp = (FileItem) it.next();
                if (fileItemTemp.isFormField()) {
                  // for other form fields that are not multipart
     //                        if (fileItemTemp.getFieldName().equals("commonName")) {
     //                            commonName = fileItemTemp.getString();
    //                        }
                } else {
                    if (fileItemTemp.getFieldName().equals("media_file")) {
                        fileItem = fileItemTemp;
                    }

                }
            }
        }
    } catch (Exception e) {
    }
    return true;
}
Sign up to request clarification or add additional context in comments.

3 Comments

do you use struts 1.3.x? If so, in some way do you disable the struts multipart request processing?
the issue is that the request was already parsed by Struts, so it cannot be parsed again. Moreover, I don't succeed in finding a way to disable the struts parsing
I have used struts 1.2 for croping and uploading images
1

I have used this example to file upload using servlet and jsp is working fine for me . Click here

Example has been explained in detail and if you face any problem then ask me, I have used this.

4 Comments

the issue is that the request was already parsed by Struts, so it cannot be parsed again. Moreover, I don't succeed in finding a way to disable the struts parsing
What's your form enctype?
enctype="multipart/form-data" I have used
I don't succeed in understanding how you can parse again the request if struts does it first.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.