springmvc实现文件下载

使用springmvc实现文件下载有两种方式,都需要设置response的Content-Disposition为attachment;filename=test2.png

第一种可以直接向response的输出流中写入对应的文件流

第二种可以使用 ResponseEntity<byte[]>来向前端返回文件

一、使用response


@RestController
@RequestMapping("/download")
public class DownloadController { @RequestMapping("/d1")
public ResultVo<String> downloadFile(HttpServletResponse response){ String fileName="test.png";
try {
//获取页面输出流
ServletOutputStream outputStream = response.getOutputStream();
//读取文件
byte[] bytes = FileUtils.readFileToByteArray(new File("D:\\my-study\\test2.png"));
//向输出流写文件
//写之前设置响应流以附件的形式打开返回值,这样可以保证前边打开文件出错时异常可以返回给前台
response.setHeader("Content-Disposition","attachment;filename="+fileName);
outputStream.write(bytes);
outputStream.flush();
outputStream.close();
return ResultVoUtil.success("success");
} catch (IOException e) {
return ResultVoUtil.error(e);
} }
}

推荐使用这种方式,这种方式可以以json形式给前台返回提示信息。

二、使用ResponseEntity


@Controller
@RequestMapping("/download2")
public class DownloadController2 { private final static Logger logger= LoggerFactory.getLogger(CategoryDataController.class); @GetMapping("/d2")
public ResponseEntity<byte[]> download2(){
//获取文件对象
try {
byte[] bytes = FileUtils.readFileToByteArray(new File("D:\\my-study\\bill-admin\\test2.png"));
HttpHeaders headers=new HttpHeaders();
headers.set("Content-Disposition","attachment;filename=test2.png");
ResponseEntity<byte[]> entity=new ResponseEntity<>(bytes,headers,HttpStatus.OK);
return entity;
} catch (IOException e) {
logger.error("下载出错:",e);
return null;
}
}
}

springmvc实现文件下载的更多相关文章

  1. 基于 Nginx XSendfile + SpringMVC 进行文件下载

    转自:http://denger.iteye.com/blog/1014066 基于 Nginx XSendfile + SpringMVC 进行文件下载 PS:经过实际测试,通过 nginx 提供文 ...

  2. SpringMVC实现文件下载的两种方式及多文件下载

    1.传统方法 @RequestMapping("/download") public String download( String fileName ,String filePa ...

  3. 解决springmvc中文件下载功能中使用javax.servlet.ServletOutputStream out = response.getOutputStream();后运行出异常但结果正确的问题

    问题描述: 在springmvc中实现文件下载功能一般都会使用javax.servlet.ServletOutputStream out = response.getOutputStream();封装 ...

  4. SpringMVC,SpringBoot文件下载

    前言 最近严查security, 导致原来暴露出去的s3不能用了,不允许public的s3,暂时的折中方案是自己做跳转.于是需要在SpringMVC中实现文件下载功能. 关于文件存储的设计 文件存储通 ...

  5. springmvc实现文件下载到Android手机设备pda端

    1:首先要添加相关得jar文件,在pom.xml中 <dependency> <groupId>commons-fileupload</groupId> <a ...

  6. SpringMVC实现文件下载时,请求路径中的扩展名被省略

    问题描述 问题是这样的,我写了一个DownloadController,用来处理下载请求,预期效果如下: 客户端浏览器在访问URL -->   http://localhost:8080/ssm ...

  7. Springmvc 服务器端文件下载

    转自:http://blog.csdn.net/boneix/article/details/51303280 业务场景:点击下载后直接保存而不是打开 解决代码:前端传入url /** * 返回流 * ...

  8. SpringMVC 文本文件下载的配置

    页面: <fieldset> <legend>Download annotator list</legend> <img src="pages/to ...

  9. springMVC学习记录3-拦截器和文件上传

    拦截器和文件上传算是springmvc中比较高级一点的内容了吧,让我们一起看一下. 下面先说说拦截器.拦截器和过滤器有点像,都可以在请求被处理之前和请求被处理之到做一些额外的操作. 1. 实现Hand ...

随机推荐

  1. Git-免密提交

    全局设置git免密提交,打开git-bash输入命令: git config credential.helper store --global 单独对某个项目仓库设置时不加  --global 设置之 ...

  2. vue插槽的使用

    一.插槽的基本使用 二.具名插槽的使用 三.编译作用域的例子 四.作用域插槽 一.插槽的基本使用     1.插槽的基本使用<slot></slot>     2.插槽的默认值 ...

  3. angularJS 十六进制与字符串相互转换

    angular 将字符串数据转换为十六进制数据 /** * @Description: TODO 字符串转16进制方法 * @author wjw * @date 2019年9月18日16:35:32 ...

  4. EAC3 Spectral Extension Process

    1.overview 当使用Spectral extension时,channel中的高频部分的transform coefficients由低频部分合成. transform coefficient ...

  5. mysql5.7导出数据提示--secure-file-priv选项问题的解决方法

    mysql可使用 into outfile 参数把表中数据导出到csv,例如可用以下命令把user表的数据导出到user.csv select * from user into outfile '/t ...

  6. IDEA 运行项目、模块的多个实例

    IDEA默认只能运行同一项目|模块的一个实例. 运行多个实例: 比如springcloud的端口设置: --server.port=9001 . 当然,也可以在项目的配置文件中修改参数. 命令行.ID ...

  7. 同步块:synchronized(同步监视器对象){同步运行代码片段}

    package seday10; import seday03.Test2; /** * @author xingsir * 同步块:synchronized(同步监视器对象){需要同步运行的代码片段 ...

  8. linux下修改mysql的编码格式

    修改编码格式:https://blog.csdn.net/qq_30038111/article/details/79376137         改编码格式在配置文件中修改才有效,在命令行中修改没效 ...

  9. python+matplotlib制作雷达图3例分析和pandas读取csv操作

    1.例一 图1 代码1 #第1步:导出模块 import numpy as np import matplotlib.pyplot as plt from matplotlib import font ...

  10. 转载UUID.randomUUID()

    UUID.randomUUID()生成唯一识别码 原创 清晨-阳光zx 最后发布于2019-04-11 20:54:40 阅读数 3039 收藏 发布于2019-04-11 20:54:40 分类专栏 ...