0

文件上传适用于小文件(在默认的 2MB 限制下),但不适用于较大的文件。我在 Tomcat 8.0 上使用 JSF,并适当地修改了我的 web.xml 以增加限制。我在 javax.servlet.MultipartConfig 的构造函数中放置了断点,所以我可以看到它读取了 web.xml 配置。但是,当调用该操作时,它默认返回默认值 2MB(特别是在 Request.parseParts(...) 中,包装器的配置为空,因此使用连接器的默认值)。

WEB.xml:

<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
<multipart-config>
  <max-file-size>-1</max-file-size>
  <max-request-size>-1</max-request-size>
  <file-size-threshold>0</file-size-threshold>
</multipart-config>
</servlet>

主页.xhtml

<h:form id="contentFormId" enctype="multipart/form-data">
...
  <h:inputFile style="display:none;" id="fileUpload" value="#{bean.uploadItem}">
  </h:inputFile>
  <h:commandButton id="browse" action="#{bean.fileUploadListener}" value="Add Files" onclick="$('#contentFormId-fileUpload').click()">
  </h:commandButton>
...
</h:form>

上下文.xml

<?xml version="1.0" encoding="utf-8"?>
<Context allowCasualMultipartParsing="true" 
...
</Context>

更新 创建简化应用程序后,似乎重写库导致请求中使用了不同的容器包装器。

不重写: Request.getWrapper()返回StandardEngine[Catalina].StandardHost[localhost].StandardContext[/TestWeb].StandardWrapper[Faces Servlet]

使用 Rewrite@URLMapping注释: Request.getWrapper()返回StandardEngine[Catalina].StandardHost[localhost].StandardContext[/TestWeb].StandardWrapper[default]

所以看来我需要配置这个应用程序的默认容器,类似于配置 Faces 的方式,或者找到一种方法让 Rewrite 委托给 Faces Servlet 容器。在 Tomcat 中编辑 maxPostSize 是一种选择(更改默认值),但如果可以避免的话,我不想采用它。

4

1 回答 1

0

我不喜欢这个解决方案,但它现在符合我的目的。似乎它应该默认为 FacesServlet 的设置,因为这是重写后的最终目的地。

我的解决方案是将 multipart-config 设置移动(或复制)到 web.xml 中的默认 servlet:

<servlet>
  <servlet-name>Faces Servlet</servlet-name>
  <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
  <load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>default</servlet-name>
  <multipart-config>
    <max-file-size>-1</max-file-size>
    <max-request-size>-1</max-request-size>
    <file-size-threshold>0</file-size-threshold>
  </multipart-config>
</servlet>
于 2016-06-23T18:34:49.163 回答