Spring Boot 文件处理

Spring Boot 文件处理

对于文件上传或者下载,我们需要关注是Request Headers 或 Response Headers 中的Content-type 属性

文件上传


对于文件上传,必须使用 MultipartFile 作为请求参数并且api中consume属性要为 MULTIPART_FORM_DATA_VALUE 。代码如下

@RequestMapping(value = "/upload", method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)

public String fileUpload(@RequestParam("file") MultipartFile file) {
   return null;
}

完整的代码如下

@RestController
public class FileUploadController {
   @RequestMapping(value = "/upload", method = RequestMethod.POST, 
      consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
   
   public String fileUpload(@RequestParam("file") MultipartFile file) throws IOException {
      File convertFile = new File("/var/tmp/"+file.getOriginalFilename());
      convertFile.createNewFile();
      FileOutputStream fout = new FileOutputStream(convertFile);
      fout.write(file.getBytes());
      fout.close();
      return "File is upload successfully";
   }
}

通过PostMan来测试,需要主要的是:设置请求头head的Content-Type:multipart/form-data;填写body选择form-data,然后选择文件。详细步骤可以参考:这里
在这里插入图片描述

文件下载


文件下载

对于文件下载,要InputStreamResource类来下载文件。我们需要设置响应头的Content-Disposition的key并且还要明确响应是多媒体类型。

public ResponseEntity<Object> downloadFile() throws IOException{
    String filename = "/var/tmp/mysql.png";
    File file = new File(filename);
    
    InputStreamResource resource = new InputStreamResource(new FileInputStream(file));
    
    HttpHeaders headers = new HttpHeaders();
    headers.add("Content-Disposition", String.format("attachment; filename=\"%s\"", file.getName()));
    headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
    header.add("Prama", "no-cache");
    header.add("Expires", "0");
}

完整代码如下

    @RequestMapping(value = "/download", method = RequestMethod.GET)
    public ResponseEntity<Object> downFile() throws FileNotFoundException {
        String filename = "/var/tmp/aa.txt";
        File file = new File(filename);

        InputStreamResource resource = new InputStreamResource(new FileInputStream(file));
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Disposition", String.format("attachment; filename=\"%s\"", file.getName()));
        headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
        headers.add("Pragma", "no-cache");
        headers.add("Expires", "0");

        ResponseEntity responseEntity = ResponseEntity.ok().headers(headers).contentLength(file.length()).
                contentType(MediaType.parseMediaType("application/txt")).body(resource);

        return responseEntity;
    }

关于更多Content-type的内容,请参考:这里
也可以查看 org.springframework.util.MimeTypeUtils 类的常量

参考


Spring Boot-File Handing

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值