码迷,mamicode.com
首页 > 编程语言 > 详细

009 spring boot中文件的上传与下载

时间:2018-10-01 01:10:20      阅读:219      评论:0      收藏:0      [点我收藏+]

标签:io操作   txt   and   文件   comm   tps   NPU   work   中文   

一:任务

1.任务

  文件的上传

  文件的下载

 

二:文件的上传

1.新建一个对象

  FileInfo.java

 1 package com.cao.dto;
 2 
 3 public class FileInfo {
 4     private String path;
 5     public FileInfo(String path) {
 6         this.path=path;
 7     }
 8     public String getPath() {
 9         return path;
10     }
11     public void setPath(String path) {
12         this.path = path;
13     }
14     
15 }

 

2.新建控制器

 1 package com.cao.web.controller;
 2 
 3 import java.io.File;
 4 import java.io.IOException;
 5 import java.util.Date;
 6 
 7 import org.springframework.web.bind.annotation.PostMapping;
 8 import org.springframework.web.bind.annotation.RequestMapping;
 9 import org.springframework.web.bind.annotation.RestController;
10 import org.springframework.web.multipart.MultipartFile;
11 
12 import com.cao.dto.FileInfo;
13 
14 @RestController
15 @RequestMapping("/file")
16 public class FileController {
17     @PostMapping
18     public FileInfo uploadFile(MultipartFile fileKey) throws Exception {
19         System.out.println("fileName: "+fileKey.getName());
20         System.out.println("OriginalFilename: "+fileKey.getOriginalFilename());
21         System.out.println("size: "+fileKey.getSize());
22         //将要存储在controller的目录下
23         String folder="E:\\workspace-sts-3.9.5.RELEASE\\it-security-demo\\src\\main\\java\\com\\cao\\web\\controller";
24         File localFile=new File(folder,new Date().getTime()+".txt");
25         fileKey.transferTo(localFile);
26         //写入流中,这个主要用于写入其他的地方,例如服务器等,这里不写了
27         //fileKey.getInputStream()
28         return new FileInfo(localFile.getAbsolutePath());
29     }
30 }

 

3.测试类

 1 /**
 2      * 测试文件的上传
 3      * @throws Exception 
 4      */
 5     @Test
 6     public void whenUploadSuccess() throws Exception {
 7         String result=mockMvc.perform(MockMvcRequestBuilders.fileUpload("/file")
 8                 .file(new MockMultipartFile("fileKey","test.txt","multipart/form-data","hello".getBytes("UTF-8"))))
 9                 .andExpect(MockMvcResultMatchers.status().isOk())
10                 .andReturn().getResponse().getContentAsString();
11         System.out.println("result="+result);
12     }

 

4.控制台

  技术分享图片

  存储到的现象

  技术分享图片

 

三:文件的下载

1.添加io操作的包

1           <dependency>
2             <groupId>commons-io</groupId>
3             <artifactId>commons-io</artifactId>
4           </dependency>

 

2.文件下载的程序

 1 package com.cao.web.controller;
 2 
 3 import java.io.File;
 4 import java.io.FileInputStream;
 5 import java.io.IOException;
 6 import java.io.InputStream;
 7 import java.io.OutputStream;
 8 import java.util.Date;
 9 
10 import javax.servlet.http.HttpServletRequest;
11 import javax.servlet.http.HttpServletResponse;
12 
13 import org.apache.commons.io.IOUtils;
14 import org.springframework.web.bind.annotation.GetMapping;
15 import org.springframework.web.bind.annotation.PathVariable;
16 import org.springframework.web.bind.annotation.PostMapping;
17 import org.springframework.web.bind.annotation.RequestMapping;
18 import org.springframework.web.bind.annotation.RestController;
19 import org.springframework.web.multipart.MultipartFile;
20 
21 import com.cao.dto.FileInfo;
22 
23 @RestController
24 @RequestMapping("/file")
25 public class FileController {
26     
27     String folder="E:\\workspace-sts-3.9.5.RELEASE\\it-security-demo\\src\\main\\java\\com\\cao\\web\\controller";
28 
29     /**
30      * 文件的上传控制器
31      * @param fileKey
32      * @return
33      * @throws Exception
34      */
35     @PostMapping
36     public FileInfo uploadFile(MultipartFile fileKey) throws Exception {
37         System.out.println("fileName: "+fileKey.getName());
38         System.out.println("OriginalFilename: "+fileKey.getOriginalFilename());
39         System.out.println("size: "+fileKey.getSize());
40         //将要存储在controller的目录下
41         File localFile=new File(folder,new Date().getTime()+".txt");
42         fileKey.transferTo(localFile);
43         //写入流中,这个主要用于写入其他的地方,例如服务器等,这里不写了
44         //fileKey.getInputStream()
45         return new FileInfo(localFile.getAbsolutePath());
46     }
47     
48     /**
49      * 文件的下载
50      */
51     @GetMapping("/{id}")
52     public void downFile(@PathVariable String id, HttpServletRequest request,HttpServletResponse response) {
53         try(
54             InputStream inputStream=new FileInputStream(new File(folder,id+".txt"));
55             OutputStream outputStream=response.getOutputStream();    
56             ) 
57         {
58             response.setContentType("application/x-download");
59             response.addHeader("Content-Disposition", "attachment;filename=test.txt");
60             IOUtils.copy(inputStream, outputStream);
61             outputStream.flush();
62         } catch (Exception e) {
63             // TODO: handle exception
64         }
65     }
66     
67     
68 }

 

3.在浏览器上访问

  技术分享图片

 

009 spring boot中文件的上传与下载

标签:io操作   txt   and   文件   comm   tps   NPU   work   中文   

原文地址:https://www.cnblogs.com/juncaoit/p/9733763.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!