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. element 表格里的input点击回车聚焦下个input

    <template> <card> <el-table ref="singleTable" :data="tableData" h ...

  2. 关于Win32串口

    因为近段时间接触Hid相对来说多一些,由此忽略了串口中获取cbInQue这个重要的东西,下面是错误代码 // Win32SerialPortLib.cpp : 定义 DLL 应用程序的导出函数. // ...

  3. 松软科技web教程:JavaScript HTML DOM 事件监听器

    addEventListener() 方法 实例 添加当用户点击按钮时触发的事件监听器: document.getElementById("myBtn").addEventList ...

  4. 2.Map中hashMap和hashTable两个的对比

    我们来对比一下hashMap和hashTable吧: 1.hashMap允许键.值可以为空,hashTable键和值都不可以为空,为什么这样呢,我们来看一下他们的put方法的源码. 先看hashMap ...

  5. caffe.bin用法

    $ ./build/tools/caffe.bin caffe.bin: command line brew usage: caffe <command><aegs> comm ...

  6. 力扣MYSQL练习

    176编写一个 SQL 查询,获取 Employee 表中第二高的薪水(Salary) . select IFNULL((SELECT distinct salary from employee or ...

  7. LabVIEW随笔2_毕业了

    08年研究生毕业了,在原来公司的兼职也结束了,开始真正的工作生涯了. 在此,非常感恩我的导师綦院长,体谅我的困楚之处,在我的研究生学习期间给予我的宽容和照顾.不料,2014年,恩师五十有余,却突然离世 ...

  8. Python单元测试unittest与HTMLTestRunner报告生成

    本文为简单介绍,使用python自带模块unittest来进行单元测试 首先我们有一个需要测试的类,employee.py  定义了涨薪的方法.我们需要测试这个类的功能是否正确. class Empl ...

  9. 【外文阅读】Web Development in 2020: What Coding Tools You Should Learn---Quincy Larson

    原文链接:https://mail.qq.com/cgi-bin/readtemplate?t=safety&check=false&gourl=https%3A%2F%2Fwww.f ...

  10. jsoup学习待续

    1.Jsoup简介 Jsoup是一个java html解析器.它是一个用于解析HTML文档的java库.Jsoup提供api来从URL或HTML文件中提取和操作数据.它使用DOM,CSS和类似 Jqu ...