图片演示

springboot - 文件下载
image.png

ftl层

<a href="download?filename=${src}">
    <img src="${src}">
</a>

Controller层

  // 文件下载
    @GetMapping(value="/download")
    public ResponseEntity<byte[]> download(HttpServletRequest request, @RequestParam("filename") String filename) throws Exception{
        // 下载路径

        File file = new File(path+File.separator+filename);
        HttpHeaders headers = new HttpHeaders();
        // 解决中文乱码
        String downloadfile =  new String(filename.getBytes("UTF-8"),"iso-8859-1");
        // 以下载方式打开文件
        headers.setContentDispositionFormData("attachment", downloadfile);
        // 二进制流
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);

        return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),headers,HttpStatus.CREATED);

    }

相关文章:

  • 2021-08-21
  • 2022-12-23
  • 2022-01-31
  • 2021-07-29
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-04-29
  • 2022-02-20
  • 2021-06-06
  • 2021-05-30
  • 2022-12-23
  • 2021-08-24
  • 2021-06-27
相关资源
相似解决方案