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
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
添加bean
2.
后台接受
3.
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
- spring3.0 文件上传
- Spring3 MVC 文件上传
- spring3 文件上传
- Spring3.1 文件上传
- Spring3 文件上传
- Spring3上传文件
- Spring3 REST 上传多个文件
- Spring3.0 文件上传与下载显示
- spring3 +commons-fileupload+uploadify 上传文件
- Spring3文件上传,提速你的Web开发
- 基于Spring3 MVC实现基于HTML form表单文件上传
- Spring3文件上传,提速你的Web开发
- Spring3.0 入门进阶(5):文件的上传与下载
- Spring3.0.5MVC + ajaxfileupload无刷新文件上传
- Struts2.3.5+Hibernate3+Spring3.1基于注解实现的多文件上传,下载
- SpringMVC案例3----spring3.0项目拦截器、ajax、文件上传应用
- 基于spring3.0 MVC框架的文件上传(源码+解析)
- 基于Spring3 MVC实现基于form表单上传Excel文件,批量导入数据!
- Python类里的__init__方法函数,Python类的构造函数
- C#实现MD5算法
- Search for a Range
- 【VC疑难】Visual Studio has encountered an exception.This may be caused by an extension.
- 141.Linked List Cycle
- Spring3上传文件
- 正则表达式-速记理解技巧
- 串的kmp模式匹配
- 10-语言入门-10-韩信点兵
- C#:几种数据库的大数据批量插入
- 关于Linux邮件列表的订阅与取消订阅具体方法
- 9个主流的开源许可协议
- 异常与自定义异常
- Sicily 1196. Conflict