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. No Delegate set : lost message:libpng error: Not a PNG file

    当出现这个问题时,是因为本来是jpg或其他格式的图片存成了png导致的.或者有的图片本来就是jpg的,Android Studio一编译,发现不是png才造成了这个问题.解决这个问题可以在Androi ...

  2. JavaScript DOM–元素操作

    获取元素 根据 ID 获取元素 语法: document.getElementById(id) <div id='time'>2020-01-09</div> <scri ...

  3. js -- 日期时间格式化

    /** * js日期时间格式化 * @param date 时间读对象 * @param format 格式化字符串 例如:yyyy年MM月dd日 hh时mm分ss秒 * @returns {stri ...

  4. centOS7中启动MySQL数据库提示: Failed to start mysqld.service: Unit not foundc

    现象: 在centOS7中启动MySQL数据库提示: Failed to start mysqld.service: Unit not found [明明已经安装了,为什么提示不存在呢?] 原因: 在 ...

  5. IDEA常用操作链接

    idea中svn的提交.更新等操作 https://jingyan.baidu.com/article/375c8e19e3c47a25f3a22955.html Idea 部署SVN详细步骤以及上传 ...

  6. DVWA的安装及报错解决

    PS:我是在wamp5集成环境中搭建的 1.解压下载好的DVWA安装包到www目录下 DVWA安装包: https://pan.baidu.com/s/1ivnwiH53gIV5jWU5IyeD0Q ...

  7. MySQL学习(八)删除表数据

    表空洞的产生 删除某个行数据 或删除某个页     如下图所示,这个删除过程只是标记了某行的位置为删除,假如此时在300与600之间插入了一行数据,那么 同理,当删除某个页时,该页就会被复用.所以当删 ...

  8. PHP基础学习笔记4

    一.日期 1.1 date()函数 语法:string date ( string $format [, int $timestamp ] ) 参数:参数描述format必需,规定时间戳的格式:tim ...

  9. Oracle常用函数记录

    Oracle函数 --schema:hcf --不带任何参数 http://www.cnblogs.com/wuyisky/archive/2010/05/11/oracle_function.htm ...

  10. Unity手机端手势基本操作

    主要有单指移动3D物体.单指旋转3D物体.双指缩放3D物体. 基类 using UnityEngine; using System.Collections; /// <summary> / ...