Spring3上传文件

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



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值