返回介绍

11.9 用 Servlet 3 及其更高版本上传文件

发布于 2025-04-22 20:10:04 字数 4808 浏览 0 评论 0 收藏

有了 Servlet 3,就不需要 Commons FileUpload 和 Commons IO 元件了。在 Servlet 3 及其以上版本的容器中进行服务器端文件上传的编程,是围绕着注解类型 MultipartConfig 和 javax.servlet.http.Part 接口进行的。处理已上传文件的 Servlets 必须以 @MultipartConfig 进行注解。

下列是可能在 MultipartConfig 注解类型中出现的属性,它们都是可选的。

  • maxFileSize:上传文件的最大容量,默认值为−1,表示没有限制。大于指定值的文件将会遭到拒绝。

  • maxRequestSize:表示多部分 HTTP 请求允许的最大容量,默认值为−1,表示没有限制。

  • location:表示在 Part 调用 write 方法时,要将已上传的文件保存到磁盘中的位置。

  • fileSizeThreshold:上传文件超出这个容量界限时,会被写入磁盘。

Spring MVC 的 DispatcherServlet 处理大部分或者所有请求。令人遗憾的是,如果不修改源代码,将无法对 Servlet 进行注解。但值得庆幸的是,Servlet 3 中有一种比较容易的方法,能使一个 Servlet 变成一个 MultipartConfig Servlet,即给部署描述符(web.xml)中的 Servlet 声明赋值。以下代码与用 @MultipartConfig 给 DispatcherServlet 进行注解的效果一样。

<servlet>
  <servlet-name>springmvc</servlet-name>
  <servlet-class>
    org.springframework.web.servlet.DispatcherServlet
  </servlet-class>
  <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
      /WEB-INF/config/springmvc-config.xml
    </param-value>
  </init-param>
  <multipart-config>
    <max-file-size>20848820</max-file-size>
    <max-request-size>418018841</max-request-size>
    <file-size-threshold>1048576</file-size-threshold>
  </multipart-config>
</servlet>

此外,还需要在 Spring MVC 配件文件中使用一个不同的多部分解析器,像下面这样:

< bean id="multipartResolver"
    class="org.springframework.web.multipart.support.
→ StandardServletMultipartResolver">
< /bean>

upload2 应用程序展示了如何在 Servlet 3 及其更高版本的容器中处理文件上传问题。这是从 upload1 改写过来的,因此,domain 和 controller 类都非常相似。唯一的区别在于,现在的 web.xml 文件中包含了一个 multipart-config 元素。清单 11.6 展示了 upload2 的 web.xml 文件。

清单 11.6 upload2 的 web.xml 文件

< ?xml version="1.0" encoding="UTF-8"?>
< web-app version="3.0"
  xmlns="http://java.sun.com/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
→ http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

  < servlet>
    < servlet-name>springmvc< /servlet-name>
    < servlet-class>
      org.springframework.web.servlet.DispatcherServlet
    < /servlet-class>
    < init-param>
      < param-name>contextConfigLocation< /param-name>
      < param-value>
        /WEB-INF/config/springmvc-config.xml
      < /param-value>
    < /init-param>
    < load-on-startup>1< /load-on-startup>
    < multipart-config>
      < max-file-size>20848820< /max-file-size>
      < max-request-size>418018841< /max-request-size>
      < file-size-threshold>1048576< /file-size-threshold>
    < /multipart-config>
  < /servlet>

  < servlet-mapping>
    < servlet-name>springmvc< /servlet-name>
    < url-pattern>/< /url-pattern>
  < /servlet-mapping>
< /web-app>

清单 11.7 展示了 upload2 的 Spring MVC 配置文件。

清单 11.7 upload2 的 Spring MVC 配置文件

< ?xml version="1.0" encoding="UTF-8"?>
< beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:p="http://www.springframework.org/schema/p"
  xmlns:mvc="http://www.springframework.org/schema/mvc"
  xmlns:context="http://www.springframework.org/schema/context"
  xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-
→ context.xsd">

  < context:component-scan base-package="controller" />
  < mvc:annotation-driven />

  < mvc:resources mapping="/css/ **" location="/css/" />
  < mvc:resources mapping="/ *.html" location="/" />
  < mvc:resources mapping="/image/ **" location="/image/" />
  < mvc:resources mapping="/file/ **" location="/file/" />

  < bean id="viewResolver"
    class="org.springframework.web.servlet.view.
→ InternalResourceViewResolver">
    < property name="prefix" value="/WEB-INF/jsp/" />
    < property name="suffix" value=".jsp" />
  < /bean>

  < bean id="multipartResolver"
    class="org.springframework.web.multipart.support.
→ StandardServletMultipartResolver">
  < /bean>
< /beans>

如果要对这个应用程序进行测试,请在浏览器中访问以下 URL:

http://localhost:8080/upload2/input-product

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。