参考资料:https://spring-mvc.linesh.tw/
1. SpringMVC 文件上传与下载
SpringMVC 框架提供了 MultipartFile
对象,该对象表示上传的文件,要求变量名称必须和表单 file 标签的
name属性名称相同。
- 在 pom.xml 文件中导入依赖
commons-fileupload
1 2 3 4 5 6 7 8 9 10 11 12 13
| <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.4</version> </dependency>
<dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.5</version> </dependency>
|
- 在 springmvc.xml 中配置文件解析器对象,
id 名称必须为:multipartResolver
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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="com.demo"/>
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp"/> </bean>
<mvc:annotation-driven />
<mvc:default-servlet-handler/>
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize" value="50000000"/> </bean>
</beans>
|
- 创建 jsp 页面
post + multipart/form-data
1 2 3 4 5 6 7 8
| <form action="user/fileupload" method="post" enctype="multipart/form-data"> 选择文件:<input type="file" name="upload" /><br/> <input type="submit" value="上传" /> </form>
<img src="http://localhost:8081/uploadfile/images/${filename}">
|
- 编写测试方法
准备工作:图片服务器要开着。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| @Controller @RequestMapping("/img") public class ImgController { @RequestMapping("/upload") public String upload(MultipartFile upload) throws Exception { System.out.println("springmvc文件上传...");
String path = "G:/upload"; File file = new File(path); if(!file.exists()){ file.mkdirs(); }
String filename = upload.getOriginalFilename(); String uuid = UUID.randomUUID().toString().replace("-", ""); filename = uuid+"_"+filename; upload.transferTo(new File(path,filename));
return "success"; } @RequestMapping("download") public void download(String fileName, HttpServletResponse response)throws Exception{ File file = new File("D:\\server\\apache-tomcat-8.5.31\\webapps\\upload\\"+fileName); response.setHeader("Content-Disposition", "attachment; filename="+file.getName());
FileInputStream in = new FileInputStream(file); byte[] buf = new byte[1024]; int len = 0;
while( (len=in.read(buf))!=-1 ){ response.getOutputStream().write(buf, 0, len); } } }
|