如何在运行在Tomcat上的Servlet过滤器中使用HttpServletRequest#getParts()? [英] How to use HttpServletRequest#getParts() in a servlet filter running on Tomcat?
问题描述
我想在我的JSF应用程序中上传一个文件。我正在使用 Filter
和 HttpServletRequestWrapper
来访问上传文件。
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 =在web应用程序的
。 中,输入true
在 Webapp / META-INF / context.xml
或 Tomcat / conf中的< Context>
/server.xml
$ b
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()
a>只能在 HttpServlet
与 @MultipartConfig
注释。另请参阅 < Context>
元素:
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:
allowCasualMultipartParsing
设置为
true
如果Tomcat应该自动解析multipart / form-data $ c $即使在目标servlet不是''的情况下调用
HttpServletRequest.getPart *
或HttpServletRequest.getParameter *
t用@MultipartConfig
注释标记(详情参见Servlet Specification 3.0,Section 3.2)。请注意,除false
之外的任何设置都会导致Tomcat的行为方式不符合技术规范。默认是false
。
allowCasualMultipartParsing
Set to
true
if Tomcat should automatically parsemultipart/form-data
request bodies whenHttpServletRequest.getPart*
orHttpServletRequest.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 thanfalse
causes Tomcat to behave in a way that is not technically spec-compliant. The default isfalse
.
另见:
- Tomcat 7问题49711
- 在Servlet 3.0中上传文件
- 在JSF 2.0和Servlet 3.0中上传文件
- Tomcat 7 issue 49711
- Uploading files in Servlet 3.0
- Uploading files in JSF 2.0 and Servlet 3.0
See also:
无关具体问题,以下绝对不对:
Unrelated to the concrete problem, the following is definitely not right:
byte[] b = new byte[(int) p.getSize()];
p.getInputStream().read(b);
params.put(p.getName(), new String[]{new String(b)});
首先,您不尊重客户端指定的字符编码-if any。其次,二进制文件会失败。
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屋!