Java servlet: issue with multipart/form

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


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;
}

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.

链接地址: http://www.djcxy.com/p/64192.html

上一篇: 需要知道T的完整定义的ptr <T>?

下一篇: Java servlet:与multipart / form相关的问题