SpringBoot文件的上传与下载

⒈文件实体类

 1 package cn.coreqi.security.entities;
 2 
 3 public class FileInfo {
 4 
 5     private String path;
 6 
 7     public FileInfo(String path) {
 8         this.path = path;
 9     }
10 
11     public String getPath() {
12         return path;
13     }
14 
15     public void setPath(String path) {
16         this.path = path;
17     }
18 }

⒉控制器代码

 1 package cn.coreqi.security.controller;
 2 
 3 import cn.coreqi.security.entities.FileInfo;
 4 import org.apache.tomcat.util.http.fileupload.IOUtils;
 5 import org.springframework.web.bind.annotation.GetMapping;
 6 import org.springframework.web.bind.annotation.PathVariable;
 7 import org.springframework.web.bind.annotation.PostMapping;
 8 import org.springframework.web.bind.annotation.RestController;
 9 import org.springframework.web.multipart.MultipartFile;
10 
11 import javax.servlet.http.HttpServletRequest;
12 import javax.servlet.http.HttpServletResponse;
13 import java.io.*;
14 import java.util.Date;
15 
16 @RestController
17 public class FileController {
18 
19     @PostMapping("/file")
20     public FileInfo upload(MultipartFile file) throws IOException {
21         System.out.println(file.getName()); //文件名
22         System.out.println(file.getOriginalFilename()); //原始文件名
23         String folder = "d:/test";
24         File localFile = new File(folder,new Date().getTime() + ".txt");
25         file.transferTo(localFile); //将上传的文件写入到本地的文件中
26         return new FileInfo(localFile.getAbsolutePath());   //绝对路径
27     }
28 
29     @GetMapping("/file/{id}")
30     public void download(@PathVariable String id, HttpServletRequest request, HttpServletResponse response){
31         try(
32                 InputStream inputStream = new FileInputStream(new File("d:/test/1553692860875.txt"));
33                 OutputStream outputStream = response.getOutputStream();
34                 ) {
35             response.setContentType("application/x-download");
36             response.addHeader("Content-Disposition","attachment;filename=test.txt");
37             IOUtils.copy(inputStream,outputStream);
38             outputStream.flush();
39 
40         } catch (FileNotFoundException e) {
41             e.printStackTrace();
42         } catch (IOException e) {
43             e.printStackTrace();
44         }
45     }
46 }

⒊测试

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

 

转载于:https://www.cnblogs.com/fanqisoft/p/10611005.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值