springmvc multiple files / folders upload and download

Inject dependencies

 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          <-! the JSTL (JSP Standard the Tag Library, JSP Standard Tag Library) is a continuous improvement of the JSP tags open source ->
 41 is          <dependency>
 42 is              <the groupId> the javax.servlet </ the groupId>
 43 is              <the artifactId> JSTL </ the artifactId>
 44 is              <Version> 1.2 </ Version>
 45          </ dependency>
 46 is          <-! class and a method comprising the Servlet ->
 47          <dependency>
 48              <the groupId> the javax.servlet </ the groupId>
 49              <the artifactId> the javax.servlet-API </ the 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>
View Code

 

xml configuration

 <!-- 多部分文件上传 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>

 

Core Source

 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, the HttpServletResponse Response) throws IOException {
 35          String STR = "D: \\ \\ Documents \\ Pictures" ;
 36          the response.setContentType ( "file application / OCTET-Stream"); // Browser pop Download box 
37 [          Response.AddHeader ( "the Content-Disposition", "Attachment; filename =" + filename); // the name of the specified file downloaded 
38 is          Response.AddHeader ( "the Content-the Length", "" + new new file (STR + filename) .length ()); // explain the size of the downloaded files, 
39          IOUtils.copy ( new new FileInputStream (str + filename), the Response.
getOutputStream());
40         return null;
41     }
42 }

 

Guess you like

Origin www.cnblogs.com/LiuOOP/p/11201290.html