springboot实现返回文件流
更新时间:2022年03月18日 09:10:59 作者:han1396735592
这篇文章主要介绍了springboot实现返回文件流方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
springboot返回文件流
@GetMapping(value = "/file/{fileName}")
public ResponseEntity<FileSystemResource> getFile(@PathVariable("fileName") String fileName) throws FileNotFoundException {
File file = new File(filePath, fileName);
if (file.exists()) {
return export(file);
}
System.out.println(file);
return null;
}
public ResponseEntity<FileSystemResource> export(File file) {
if (file == null) {
return null;
}
HttpHeaders headers = new HttpHeaders();
headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
headers.add("Content-Disposition", "attachment; filename=" + file.getName());
headers.add("Pragma", "no-cache");
headers.add("Expires", "0");
headers.add("Last-Modified", new Date().toString());
headers.add("ETag", String.valueOf(System.currentTimeMillis()));
return ResponseEntity.ok().headers(headers).contentLength(file.length()).contentType(MediaType.parseMediaType("application/octet-stream")).body(new FileSystemResource(file));
}
springboot返回二进制文件流
@GetMapping("/getTemplateFile")
@ApiOperation("数据模板下载")
public ResponseEntity<byte[]> downFile(HttpServletRequest request) throws IOException {
File file = new File("C/AA");
filename = getFilename(request, filename);
//设置响应头
HttpHeaders headers = new HttpHeaders();
//通知浏览器以下载的方式打开文件
headers.setContentDispositionFormData("attachment", filename);
//定义以流的形式下载返回文件数据
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
//使用springmvc框架的ResponseEntity对象封装返回数据
return new ResponseEntity<>(FileUtils.readFileToByteArray(file), headers, HttpStatus.OK);
}
/**
* 根据浏览器的不同进行编码设置
*
* @param request 请求对象
* @param filename 需要转码的文件名
* @return 返回编码后的文件名
* @throws IOException
*/
public String getFilename(HttpServletRequest request, String filename) throws IOException {
//IE不同版本User-Agent中出现的关键词
String[] IEBrowserKeyWords = {"MSIE", "Trident", "Edge"};
//获取请求头代理信息
String userAgent = request.getHeader("User-Agent");
for (String keyWord : IEBrowserKeyWords) {
if (userAgent.contains(keyWord)) {
//IE内核浏览器,统一为utf-8编码显示
return URLEncoder.encode(filename, "UTF-8");
}
}
//火狐等其他浏览器统一为ISO-8859-1编码显示
return new String(filename.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1);
}
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
MyBatisPlus PaginationInterceptor分页插件的使用详解
这篇文章主要介绍了MyBatisPlus PaginationInterceptor分页插件的使用详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2021-03-03
Elasticsearch Join字段类型简单快速上手教程
这篇文章主要为大家介绍了Elasticsearch Join字段类型简单快速上手教程详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪2022-09-09
Eclipse中导入Maven Web项目并配置其在Tomcat中运行图文详解
这篇文章主要介绍了Eclipse中导入Maven Web项目并配置其在Tomcat中运行图文详解,需要的朋友可以参考下2017-12-12
java生成csv文件乱码的解决方法示例 java导出csv乱码
这篇文章主要介绍了java生成csv文件乱码的解决方法,大家可以直接看下面的示例2014-01-01
Java读取resources目录下文件路径的九种代码示例教程
在Java开发中经常需要读取项目中resources目录下的文件或获取资源路径,这篇文章主要给大家介绍了关于Java读取resources目录下文件路径的九种代码示例教程,文中通过代码介绍的非常详细,需要的朋友可以参考下2024-07-07
SpringCloud_Sleuth分布式链路请求跟踪的示例代码
Spring Cloud Sleuth是一款针对Spring Cloud的分布式跟踪工具,本文通过实例代码介绍了SpringCloud_Sleuth分布式链路请求跟踪,感兴趣的朋友跟随小编一起看看吧2023-02-02
SpringBoot中mapper.xml文件存放的两种实现位置
这篇文章主要介绍了SpringBoot中mapper.xml文件存放的两种实现位置,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教2024-01-01


最新评论