问题描述
我想在我的 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()
在 中工作/6/api/javax/servlet/Filter.html" rel="noreferrer" target="_blank">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 应该自动解析
被调用,即使目标 servlet 没有用 multipart/form-data
请求体.getParameter*@MultipartConfig
注释标记(有关详细信息,请参阅 Servlet 规范 3.0,第 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
与具体问题无关,以下肯定是不对的:
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()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,WP2