如何在 Tomcat 上运行的 servlet 过滤器中使用 HttpServletRequest#getParts()? [英] How to use HttpServletRequest#getParts() in a servlet filter running on Tomcat?

查看:22
本文介绍了如何在 Tomcat 上运行的 servlet 过滤器中使用 HttpServletRequest#getParts()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的 JSF 应用程序中上传一个文件.我正在使用 FilterHttpServletRequestWrapper 来访问上传文件.

I would like to upload a file in my JSF application. I am using a Filter and HttpServletRequestWrapper to access the upload file.

 public MultipartRequestWrapper(HttpServletRequest request) {
    super(request);
    System.out.println("Created multipart wrapper....");
    try {
        System.out.println("Looping parts"+getParts().size());

        for (Part p : getParts()) {
            System.out.println(String.format("Part name: %1$s, contentType : %2$s", p.getName(), p.getContentType()));
            for(String header : p.getHeaderNames()){
                System.out.println("Header name : " + header + ", value : " + p.getHeader(header));
            }
            byte[] b = new byte[(int) p.getSize()];
            p.getInputStream().read(b);
            params.put(p.getName(), new String[]{new String(b)});
        }
    } catch (IOException ex) {
        ex.printStackTrace();
        Logger.getLogger(MultipartRequestWrapper.class.getName()).log(Level.SEVERE, null, ex);
    } catch (ServletException ex) {
         ex.printStackTrace();
        Logger.getLogger(MultipartRequestWrapper.class.getName()).log(Level.SEVERE, null, ex);
    }

然而,getParts() 返回一个空集合.如何在 Tomcat 7.0.8 的 servlet 过滤器中启用 multipart/form-data 解析?

However, getParts() returns an empty collection. How can I enable multipart/form-data parsing in a servlet filter in Tomcat 7.0.8?

推荐答案

为了得到 HttpServletRequest#getParts()Filter 在Tomcat中,需要设置allowCasualMultipartParsing="true" 在 webapp 的 Webapp/META-INF/context.xml 或 Tomcat/conf/server.xml 中的 a> 元素.

In order to get HttpServletRequest#getParts() to work in a Filter in Tomcat, you need to set allowCasualMultipartParsing="true" in the webapp's <Context> element in Webapp/META-INF/context.xml or Tomcat/conf/server.xml.

<Context ... allowCasualMultipartParsing="true">

因为根据 servlet 3.0 规范 HttpServletRequest#getParts() 应该只在 HttpServlet@MultipartConfig 注释.另请参阅 的文档 元素:

Because as per the servlet 3.0 specification the HttpServletRequest#getParts() should only be available inside a HttpServlet with the @MultipartConfig annotation. See also the documentation of the <Context> element:

设置为true,如果HttpServletRequest.getPart*HttpServletRequest 时Tomcat 应该自动解析multipart/form-data 请求体.getParameter* 被调用,即使目标 servlet 没有用 @MultipartConfig 注释标记(有关详细信息,请参阅 Servlet 规范 3.0,第 3.2 节).请注意,除 false 之外的任何设置都会导致 Tomcat 以不符合技术规范的方式运行.默认为 false.

allowCasualMultipartParsing

Set to true if Tomcat should automatically parse multipart/form-data request bodies when HttpServletRequest.getPart* or HttpServletRequest.getParameter* is called, even when the target servlet isn't marked with the @MultipartConfig annotation (See Servlet Specification 3.0, Section 3.2 for details). Note that any setting other than false causes Tomcat to behave in a way that is not technically spec-compliant. The default is false.

另见:

  • Tomcat 7 问题 49711
  • 在 Servlet 3.0 中上传文件
  • 在 JSF 2.0 和Servlet 3.0
  • 与具体问题无关,以下肯定是不对的:

    byte[] b = new byte[(int) p.getSize()];
    p.getInputStream().read(b);
    params.put(p.getName(), new String[]{new String(b)});
    

    首先,您没有遵守客户端指定的字符编码 - 如果有的话.其次,这对于二进制文件会失败.

    First, you are not respecting the character encoding specified by the client -if any. Second, this will fail for binary files.

    这篇关于如何在 Tomcat 上运行的 servlet 过滤器中使用 HttpServletRequest#getParts()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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