SpringMVC
来源:互联网 发布:python数据类型 编辑:程序博客网 时间:2024/06/08 11:29
SpringMVC为文件上传提供了直接支持,这种支持是通过即插即用的MultipartResolver实现的。
Spring用Jakarta Commons FileUpload技术实现了一个MultipartResolver实现类:CommonsMultipartResolver
。
SpringMVC上下文中默认没有装配MultipartResolver,因此默认情况下不能处理文件上传工作,如果想使用Spring的文件上传功能,需在上下文中配置MultipartResolver。
【1】CommonsMultipartResolver
实现类如下所示,继承了CommonsFileUploadSupport并实现了MultipartResolver和ServletContextAware接口。
public class CommonsMultipartResolver extends CommonsFileUploadSupport implements MultipartResolver, ServletContextAware { .... }
【2】加入相关jar
【3】配置CommonsMultipartResolver
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!--需与jsp页面编码保持一致--> <property name="defaultEncoding" value="UTF-8"></property> <!--限制上传大小--> <property name="maxUploadSize" value="102400000"></property> </bean>
其他属性如下图所示:
属性解释如下:
resolveLazily:延迟解析,默认为false--立即解析multipart request;defaultEncoding:解析请求的默认字符编码 ; 默认值为"ISO-8859-1"。通常设置为"UTF-8";maxUploadSize:文件上传最大值; 默认值为 -1(表示没有限制);maxUploadSizePerFile:每个文件上传最大值;默认值为 -1(表示没有限制);maxInMemorySize:存储在内存的最大值;默认值为10240B(10KB);uploadTempDir:上传文件的临时目录;默认值为WEB应用程序的临时目录;servletContext:the servletContext to use;
【4】页面测试代码
<form action="testFileUpload" method="POST" enctype="multipart/form-data"> File: <input type="file" name="file"/> Desc: <input type="text" name="desc"/> <input type="submit" value="Submit"/></form>
【5】后台测试代码
@RequestMapping("/testFileUpload") public String testFileUpload(@RequestParam("desc") String desc, @RequestParam("file") MultipartFile file) throws IOException{ if (!file.isEmpty()) { System.out.println("desc: " + desc); System.out.println("OriginalFilename(原始文件名字): " + file.getOriginalFilename()); System.out.println("InputStream(获取的文件输入流): " + file.getInputStream()); System.out.println("文件大小为(单位为字节Byte): " + file.getSize()); System.out.println("文件内容类型为: " + file.getContentType()); file.transferTo(new File("D:\\temDirectory\\"+file.getOriginalFilename())); } return "success"; }
如上述代码所示,可以拿到文件名与输入流以及文件大小!!!
最后一个方法很有意思,可以直接保存到目标路径下的文件:
void org.springframework.web.multipart.MultipartFile.transferTo(File dest) throws IOException, IllegalStateException
API说明如下:
Transfer the received file to the given destination file. This may either move the file in the filesystem, copy the file in the filesystem, or save memory-held contents to the destination file. If the destination file already exists, it will be deleted first. If the file has been moved in the filesystem, this operation cannot be invoked again. Therefore, call this method just once to be able to work with any storage mechanism.
SpringMVC上传文件原理分析与两种不同文件处理方式对比,参考:
http://www.cnblogs.com/dongying/p/4388464.html;
http://www.cnblogs.com/dongying/p/4390560.html
0 0
- SpringMVC
- springmvc
- SpringMVC
- SpringMVC
- springMvc
- springMVC
- springmvc
- springMVC
- springMVC
- springmvc
- SpringMVC
- SpringMVC
- springMvc
- springmvc
- springmvc
- SpringMVC
- Springmvc
- springmvc
- Java中的数据类型
- 基础知识(一)
- 快速排序
- 单链表(合并单链表)
- C字符串函数归总
- SpringMVC
- 商品展示案例
- L0、L1与L2范数各自功能
- 回溯-子集问题
- 数据库 用sql语句创建表
- 安装Visual studio注意事项
- 编码:隐匿在计算机背后的语言(1-3章)
- 【计算机原理】并发 并行 异步 同步 阻塞 非阻塞
- Redis分布式事务锁实现