Java servlet:multipart/form-data表单问题 [英] Java servlet: issue with multipart/form-data form

查看:153
本文介绍了Java servlet:multipart/form-data表单问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含多个<input type='text'><input type='file'>字段的multipart/form-data表单.

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

我使用此代码

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

通过运行第一个代码段,执行else,但是最后multipartItems为null.

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

由于这个原因,第二个代码段中的for永远不会执行.

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

我不知道为什么会有这种行为.我正在使用Struts 1.3.10

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

编辑

如何检查struts是否已经解析了请求?

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?

编辑2

我有一个动态格式,以json格式编码.我有一个用于json和隐藏属性的表单bean.然后,我手动"解析json.一切正常,但是现在我必须添加输入type = file字段并使用multipart/form-data编码类型.

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.

为了防止对struts请求进行解析,我将其放在web.xml中:

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>

但是它似乎不起作用

推荐答案

初始化FileItem,如下所示:

Initialize a FileItem, like below:

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

这篇关于Java servlet:multipart/form-data表单问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆