首页 > 编程语言 > 详细

springboot文件下载

时间:2019-09-19 12:58:51      阅读:107      评论:0      收藏:0      [点我收藏+]
public ResponseEntity<FileSystemResource> export(File file) {
    if (file == null) {
        return null;
    }
    HttpHeaders headers = new HttpHeaders();
    headers.add("Content-Disposition", "attachment;filename=" + file.getName());
    return ResponseEntity
            .ok()
            .headers(headers)
            .contentLength(file.length())
            .contentType(MediaType.parseMediaType("application/octet-stream"))
            .body(new FileSystemResource(file));
}

使用方式如下:

@RequestMapping("/downLoadImg")
    public ResponseEntity<FileSystemResource> downLoadImg(HttpServletRequest request) {
        String imgPath = request.getParameter("img");
        //文件路径
        String filePath = SDKUtils.getImgPath() + imgPath;
        return export(new File(filePath));
    }

springboot文件下载

原文:https://www.cnblogs.com/bdqnquliang/p/11548342.html

(1)
(1)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!