- 内容提要
- 作者简介
- 译者简介
- 前言
- HTTP
- Servlet 和 JSP
- 下载 Spring 或使用 STS 与 Maven/Gradle
- 手动下载 Spring
- 使用 STS 和 Maven/Gradle
- 下载 Spring 源码
- 本书内容简介
- 下载示例应用
- 第 1 章Spring 框架
- 第 2 章模型 2 和 MVC 模式
- 第 3 章Spring MVC 介绍
- 第 4 章基于注解的控制器
- 第 5 章数据绑定和表单标签库
- 第 6 章转换器和格式化
- 第 7 章验证器
- 第 8 章表达式语言
- 第 9 章JSTL
- 第 10 章国际化
- 第 11 章上传文件
- 第 12 章下载文件
- 第 13 章应用测试
- 附录 A Tomcat
- 附录 B Spring Tool Suite 和 Maven
- 附录 C Servlet
- 附录 D JavaServer Pages
- 附录 E 部署描述符
11.9 用 Servlet 3 及其更高版本上传文件
有了 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
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论