SpringBoot/SpringMVC文件下载方式

本篇文章引用外网博客代码,共描述SpringMVC下三种文件下载方式,本人测试在SpringBoot(2.0以上版本)正常使用.
引用博客,强烈推荐https://www.boraji.com.

package com.boraji.tutorial.spring.controller;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import javax.servlet.http.HttpServletResponse;

import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

/**
 * @author imssbora
 */
@Controller
public class FileDownloadController {

   private static final String FILE_PATH = "D:/e-Books/jsp_tutorial.pdf";

   @GetMapping("/")
   public String fileUploadForm(Model model) {
      return "fileDownloadView";
   }

   // Using ResponseEntity<InputStreamResource>
   @GetMapping("/download1")
   public ResponseEntity<InputStreamResource> downloadFile1() throws IOException {

      File file = new File(FILE_PATH);
      InputStreamResource resource = new InputStreamResource(new FileInputStream(file));

      return ResponseEntity.ok()
            .header(HttpHeaders.CONTENT_DISPOSITION,
                  "attachment;filename=" + file.getName())
            .contentType(MediaType.APPLICATION_PDF).contentLength(file.length())
            .body(resource);
   }

   // Using ResponseEntity<ByteArrayResource>
   @GetMapping("/download2")
   public ResponseEntity<ByteArrayResource> downloadFile2() throws IOException {

      Path path = Paths.get(FILE_PATH);
      byte[] data = Files.readAllBytes(path);
      ByteArrayResource resource = new ByteArrayResource(data);

      return ResponseEntity.ok()
            .header(HttpHeaders.CONTENT_DISPOSITION,
                  "attachment;filename=" + path.getFileName().toString())
            .contentType(MediaType.APPLICATION_PDF).contentLength(data.length)
            .body(resource);
   }

   // Using HttpServletResponse
   @GetMapping("/download3")
   public void downloadFile3(HttpServletResponse resonse) throws IOException {
      File file = new File(FILE_PATH);
       
      // 示例中使用的是pdf,实际的content-type需要根据上传文件时的content-type进行确定(最长可达255字节)
      resonse.setContentType("application/pdf");
      resonse.setHeader("Content-Disposition", "attachment;filename=" + file.getName());
      BufferedInputStream inStrem = new BufferedInputStream(new FileInputStream(file));
      BufferedOutputStream outStream = new BufferedOutputStream(resonse.getOutputStream());
      
      byte[] buffer = new byte[1024];
      int bytesRead = 0;
      while ((bytesRead = inStrem.read(buffer)) != -1) {
        outStream.write(buffer, 0, bytesRead);
      }
      outStream.flush();
      inStrem.close();
   }
}

ResponseEntity表示HTTP响应,包含body,header以及status code.
ResponseEntity可以在RestTemplate以及@Controller中使用.

PS:
如果您觉得我的文章对您有帮助,可以扫码领取下红包或扫码支持(随意多少,一分钱都是爱),谢谢!

支付宝红包支付宝微信
764719-20190624102014468-193165117.png764719-20190624102025867-1690450943.png764719-20190624101626318-627523959.png

转载于:https://www.cnblogs.com/jason1990/p/10537085.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值