简体   繁体   English

Java servlet:multipart / form-data表单的问题

[英]Java servlet: issue with multipart/form-data form

I have a multipart/form-data form with some <input type='text'> and <input type='file'> fields. 我有一个multipart / form-data表单,其中包含一些<input type='text'><input type='file'>字段。

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. 通过运行第一段代码,执行else,但最后multipartItems为null。

For this reason, the for in the second code snippet is never executed. 因此,永远不会执行第二个代码段中的for。

I don't know why there is this behaviour. 我不知道为什么会有这种行为。 I'm using Struts 1.3.10 我正在使用Struts 1.3.10

EDIT 编辑

How can I check if struts has already parsed the request? 如何检查struts是否已解析请求?

If so, is there a way to turn off it only for a particular form? 如果是这样,有没有办法只为特定的表格关闭它?

EDIT 2 编辑2

I have a dynamic form, coded in json format. 我有一个动态表单,以json格式编码。 I have a form bean for json and for hidden properties. 我有一个用于json和隐藏属性的表单bean。 Then I parse the json "by hand". 然后我“手动”解析json。 All works perfectly, but now I have to add input type=file fields and use the multipart/form-data enctype. 一切都很完美,但现在我必须添加input type = file字段并使用multipart / form-data enctype。

To prevent struts request parsing I put in the web.xml: 为了防止struts请求解析我放入了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 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 . 我使用这个例子来使用servlet上传文件,jsp对我来说很好。 Click here 点击这里

Example has been explained in detail and if you face any problem then ask me, I have used this. 已经详细解释了示例,如果您遇到任何问题,请问我,我已经使用了这个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM