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

4

2 回答 2

2

初始化一个 FileItem,如下所示:

     FileItem fileItem = null;

然后调用这个方法

    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;
}
于 2013-09-17T10:33:52.463 回答
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.

于 2013-09-19T06:26:28.373 回答