zoukankan      html  css  js  c++  java
  • 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     }
  • 相关阅读:
    Python爬虫之记录一次下载验证码的尝试
    Python之学会测试,让开发更加高效(一)
    NLP(二十八)多标签文本分类
    NLP(二十七)开放领域的三元组抽取的一次尝试
    NLP(二十六)限定领域的三元组抽取的一次尝试
    Numpy之数据保存与读取
    TortoiseGit的首次使用
    NLP(二十五)实现ALBERT+Bi-LSTM+CRF模型
    NLP(二十四)利用ALBERT实现命名实体识别
    NLP(二十三)序列标注算法评估模块seqeval的使用
  • 原文地址:https://www.cnblogs.com/fanqisoft/p/10611005.html
Copyright © 2011-2022 走看看