springmvc 多文件/文件夹上传 下载
注入依赖
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <properties> 2 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 3 <maven.compiler.source>1.7</maven.compiler.source> 4 <maven.compiler.target>1.7</maven.compiler.target> 5 <spring.version>4.3.24.RELEASE</spring.version> 6 </properties> 7 8 <dependencies> 9 <!-- Spring --> 10 <dependency> 11 <groupId>org.springframework</groupId> 12 <artifactId>spring-core</artifactId> 13 <version>${spring.version}</version> 14 </dependency> 15 <dependency> 16 <groupId>org.springframework</groupId> 17 <artifactId>spring-context</artifactId> 18 <version>${spring.version}</version> 19 </dependency> 20 <dependency> 21 <groupId>org.springframework</groupId> 22 <artifactId>spring-web</artifactId> 23 <version>${spring.version}</version> 24 </dependency> 25 <dependency> 26 <groupId>org.springframework</groupId> 27 <artifactId>spring-context-support</artifactId> 28 <version>${spring.version}</version> 29 </dependency> 30 <dependency> 31 <groupId>org.springframework</groupId> 32 <artifactId>spring-webmvc</artifactId> 33 <version>${spring.version}</version> 34 </dependency> 35 <dependency> 36 <groupId>org.springframework</groupId> 37 <artifactId>spring-test</artifactId> 38 <version>${spring.version}</version> 39 </dependency> 40 <!-- JSTL(JSP Standard Tag Library ,JSP标准标签库)是一个不断完善的开放源代码的JSP标签 --> 41 <dependency> 42 <groupId>javax.servlet</groupId> 43 <artifactId>jstl</artifactId> 44 <version>1.2</version> 45 </dependency> 46 <!-- Servlet包含的类以及方法 --> 47 <dependency> 48 <groupId>javax.servlet</groupId> 49 <artifactId>javax.servlet-api</artifactId> 50 <version>3.0.1</version> 51 </dependency> 52 <dependency> 53 <groupId>org.hibernate</groupId> 54 <artifactId>hibernate-validator</artifactId> 55 <version>5.2.4.Final</version> 56 </dependency> 57 58 <dependency> 59 <groupId>com.google.code.gson</groupId> 60 <artifactId>gson</artifactId> 61 <version>2.8.5</version> 62 </dependency> 63 <dependency> 64 <groupId>junit</groupId> 65 <artifactId>junit</artifactId> 66 <version>4.11</version> 67 <scope>test</scope> 68 </dependency> 69 70 <!-- jackson --> 71 <dependency> 72 <groupId>com.fasterxml.jackson.core</groupId> 73 <artifactId>jackson-core</artifactId> 74 <version>2.9.3</version> 75 </dependency> 76 <dependency> 77 <groupId>com.fasterxml.jackson.core</groupId> 78 <artifactId>jackson-annotations</artifactId> 79 <version>2.9.3</version> 80 </dependency> 81 <dependency> 82 <groupId>com.fasterxml.jackson.core</groupId> 83 <artifactId>jackson-databind</artifactId> 84 <version>2.9.3</version> 85 </dependency> 86 87 <!--文件上传下载 --> 88 <dependency> 89 <groupId>commons-io</groupId> 90 <artifactId>commons-io</artifactId> 91 <version>2.4</version> 92 </dependency> 93 <dependency> 94 <groupId>commons-fileupload</groupId> 95 <artifactId>commons-fileupload</artifactId> 96 <version>1.3</version> 97 </dependency> 98 </dependencies>
xml配置
<!-- 多部分文件上传 M kb byte--> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize" value="104857600"/> <property name="defaultEncoding" value="UTF-8"></property> </bean>
核心源码
1 package com.etc.controller; 2 3 import org.apache.commons.io.IOUtils; 4 import org.springframework.stereotype.Controller; 5 import org.springframework.web.bind.annotation.RequestMapping; 6 import org.springframework.web.bind.annotation.RequestParam; 7 import org.springframework.web.multipart.MultipartFile; 8 9 import javax.servlet.http.HttpServletRequest; 10 import javax.servlet.http.HttpServletResponse; 11 import java.io.File; 12 import java.io.FileInputStream; 13 import java.io.IOException; 14 15 @Controller 16 public class FileController { 17 @RequestMapping("/upload.do") 18 public String upload(@RequestParam MultipartFile img, HttpServletRequest request) throws IOException { 19 String dir = request.getServletContext().getRealPath("/upload"); 20 img.transferTo(new File(dir + "/" + img.getOriginalFilename())); 21 return "success"; 22 } 23 24 @RequestMapping("/uploads.do") 25 public String uploads(@RequestParam MultipartFile[] img, HttpServletRequest request) throws IOException { 26 String dir = request.getServletContext().getRealPath("/upload"); 27 for (MultipartFile i : img) { 28 i.transferTo(new File(dir + "/" + i.getOriginalFilename())); 29 } 30 return "success"; 31 } 32 33 @RequestMapping("/download.do") 34 public String down(String filename, HttpServletResponse response) throws IOException { 35 String str="D:\\Documents\\Pictures\\"; 36 response.setContentType("application/octet-stream");//让浏览器弹 出下载框 37 response.addHeader("Content-Disposition", "attachment;filename=" + filename);// 指定下载的文件的名字 38 response.addHeader("Content-Length", "" + new File(str + filename).length());//说明一下下载的文件的大小, 39 IOUtils.copy(new FileInputStream(str+filename),response.getOutputStream()); 40 return null; 41 } 42 }
作者:刘认真
-------------------------------------------
新人只求记录学习生活!
如果觉得这篇文章对你有小小的帮助的话,记得在右下角点个“推荐”哦,博主在此感谢!