Spring3上传文件

来源:互联网 发布:sql查询过滤重复记录 编辑:程序博客网 时间:2024/06/06 20:20
In a Servlet 3.0–compatible web container with Spring MVC, configuring file upload support is a two-step process.
First, in the web deployment descriptor (web.xml) for the DispatchServlet definition, we need to add
a <multipart-config> section. Listing 17-52 shows the code snippet for this change.
Listing 17-52. Add File Upload Support in web.xml
<pre name="code" class="html"><?xml version="1.0" encoding="UTF-8"?><web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"<!-- Other code omitted --><!-- Processes application requests --><servlet><servlet-name>appServlet</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value></init-param><load-on-startup>1</load-on-startup><span style="color:#ff0000;"><multipart-config><max-file-size>5000000</max-file-size></multipart-config></span></servlet><!-- Other code omitted --></web-app>
The change is highlighted in bold. In Servlet 3.0, the servlet that supports file upload should be
provided with a <multipart-config> tag to configure the support. The <max-file-size> tag controls the
maximum file size allowed for upload, which is 5MB.
Second, we need to configure a bean that implements the MultipartResolver interface in the
DispatcherServlet’s WebApplicationContext. Listing 17-53 shows the bean definition that you need to
add to the file (servlet-context.xml).
Listing 17-53. Configure the MultipartResolver Support in Spring MVC
<beans:bean class="org.springframework.web.multipart.support.StandardServletMultipartResolver" id="multipartResolver"/>
Note the implementation class StandardServletMultipartResolver, which is new in Spring 3.1 for

supportinging native file upload in the Servlet 3.0 container.

好了,我们来配置

1.

web.xml添加

<servlet><servlet-name>dispatcher</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>/WEB-INF/dispatcher-servlet.xml</param-value></init-param><load-on-startup>1</load-on-startup><async-supported>true</async-supported><multipart-config><max-file-size>5000000</max-file-size></multipart-config></servlet>

添加bean

<!-- 解析上传文件 --><bean class="org.springframework.web.multipart.support.StandardServletMultipartResolver" id="multipartResolver"/>

2.

<form:form modelAttribute="user" action="Spring/formFile" method="POST" enctype="multipart/form-data"> <input name="file" type="file"/> <input value="submit" type="submit"/></form:form>

后台接受

@RequestMapping(value = "/formFile", method = RequestMethod.POST)public ModelAndView formFile(@Valid User user,@RequestParam(value="file", required=false) Part file) throws IOException{Console.WebConsole("firstName", file.getInputStream().available()+"");ModelAndView modelAndView = new ModelAndView();modelAndView.setViewName("form/form");return modelAndView;}

3.

打印

--->>>firstName=311511



0 0