2017年11月29日 10:32:20

阅读数:3907

SpringBoot的文件下载方法有很多,此处只记录使用Spring的Resource实现类FileSystemResource做下载,其余实现类照葫芦画瓢即可。 
直接上干货

1、下载部分代码

    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=" + System.currentTimeMillis() + ".xls");
headers.add("Pragma", "no-cache");
headers.add("Expires", "0");
return ResponseEntity
.ok()
.headers(headers)
.contentLength(file.length())
.contentType(MediaType.parseMediaType("application/octet-stream"))
.body(new FileSystemResource(file));
}

这段代码,我是封装在BaseController.class,其他的Controller继承该类,就可直接调用父类的下载方法,service只需要提供文件file即可。

2、实例展示

@RestController
@RequestMapping(value = "/order", method = RequestMethod.POST)
public class OrderController extends BaseController { @Autowired
private IOrderService orderService; @RequestMapping(value = "/export")
public ResponseEntity<FileSystemResource> listExport(String proNo) {
File file = orderService.listExport(proNo);
return export(file);
}
}

注意:此处使用的下载返回是FileSystemResource,所以service提供的是File。
以下是百度到的Resource其它实现类,引用来自http://elim.iteye.com/blog/2016305

ClassPathResource 可用来获取类路径下的资源文件。假设我们有一个资源文件test.txt在类路径下,我们就可以通过给定对应资源文件在类路径下的路径path来获取它,new ClassPathResource(“test.txt”)。 
FileSystemResource可用来获取文件系统里面的资源。我们可以通过对应资源文件的文件路径来构建一个FileSystemResource。FileSystemResource还可以往对应的资源文件里面写内容,当然前提是当前资源文件是可写的,这可以通过其isWritable()方法来判断。FileSystemResource对外开放了对应资源文件的输出流,可以通过getOutputStream()方法获取到。 
UrlResource可用来代表URL对应的资源,它对URL做了一个简单的封装。通过给定一个URL地址,我们就能构建一个UrlResource。 
ByteArrayResource是针对于字节数组封装的资源,它的构建需要一个字节数组。 
ServletContextResource是针对于ServletContext封装的资源,用于访问ServletContext环境下的资源。ServletContextResource持有一个ServletContext的引用,其底层是通过ServletContext的getResource()方法和getResourceAsStream()方法来获取资源的。 
InputStreamResource是针对于输入流封装的资源,它的构建需要一个输入流。

SpringBoot的文件下载的更多相关文章

  1. SpringBoot/SpringMVC文件下载方式

    本篇文章引用外网博客代码,共描述SpringMVC下三种文件下载方式,本人测试在SpringBoot(2.0以上版本)正常使用. 引用博客,强烈推荐https://www.boraji.com. pa ...

  2. SpringBoot之文件下载

    package org.springboot.controller; import org.springboot.constant.Constant; import org.springframewo ...

  3. 用Springboot实现文件下载功能

    ApiOperation(value = "下载文件", httpMethod = "GET", notes = "downloadFile" ...

  4. SpringBoot入门一:基础知识(环境搭建、注解说明、创建对象方法、注入方式、集成jsp/Thymeleaf、logback日志、全局热部署、文件上传/下载、拦截器、自动配置原理等)

    SpringBoot设计目的是用来简化Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置.通过这种方式,SpringBoot致力于在蓬勃发 ...

  5. vue下载文件

    import fileDownload from 'js-file-download' let params = { ", ", "filename":&quo ...

  6. SpringBoot(三):文件下载

    SpringBoot(三):文件下载 2017年08月02日 10:46:42 阅读数:6882 在原来的SpringBoot–uploadfile项目基础上添加文件下载的Controller: @R ...

  7. Springboot文件下载

    https://blog.csdn.net/stubbornness1219/article/details/72356632 Springboot对资源的描述提供了相应的接口,其主要实现类有Clas ...

  8. springBoot中使用使用junit测试文件上传,以及文件下载接口编写

    本篇文章将介绍如何使junit在springBoot中测试文件的上传,首先先阅读如何在springBoot中进行接口测试. 文件上传操作测试代码 import org.junit.Before; im ...

  9. 对Web(Springboot + Vue)实现文件下载功能的改进

    此为 软件开发与创新 课程的作业 对已有项目(非本人)阅读分析 找出软件尚存缺陷 改进其软件做二次开发 整理成一份博客 原项目简介 本篇博客所分析的项目来自于 ジ绯色月下ぎ--vue+axios+sp ...

随机推荐

  1. mysql 8.0 java连接报错:Unknown system variable 'query_cache_size'

    java连接mysql 8.0.11报错 java.sql.SQLException: Unknown system variable 'query_cache_size' at com.mysql. ...

  2. 配置eureka 老是报错connected time out 或者 refused connected

    报错信息总是连接错误,我指定了端口号,却不按照我指定的端口进行访问,而是访问eureka-server 的端口号是8761 ,这是因为配置有问题. 查看 类 EurekaClientConfigBea ...

  3. iOS提交iTunes审核时出现Invalid Binary错误

    xcode5编译一个xcode4时写的代码,提交iTunes审核时出错. 1.iOS提交审核时出现Invalid Binary错误 2.收到邮件: iPhone 5 Optimization Requ ...

  4. ace admin后台模板

    http://ace.jeka.by/index.html 下载: http://www.daimajiayuan.com/plus/download.php?open=0&aid=18152 ...

  5. angular中的表单数据自定义验证

    之前说过了angular是如何给表单的数据进行基本的,常用的验证的:angular学习笔记(二十)-表单验证 但是在实际工作中,这些验证是远远不够的,很多时候我们需要自定义一些验证规则,以及一些异步, ...

  6. [CoreOS 转载]CoreOS实践指南(二):架设CoreOS集群

    转载:http://www.csdn.net/article/2015-01-04/2823399 摘要:CoreOS是一个采用了高度精简的系统内核及外围定制的操作系统.ThoughtWorks的软件 ...

  7. [Windows Azure] Load Testing in Windows Azure

    The primary goal of a load test is to simulate many users accessing a web application at the same ti ...

  8. MVC的项目部署成应用程序或虚拟目录路径的问题

    1.js和css的引用出错 a.~/可以取得应用程序目录 b. ./定位到路径,./代表到本目录,../代表父级目录 2.打开页面view a. ./定位到路径 3.img src   a. ./定位 ...

  9. mysql_secure_installation

    安装完mysql-server 会提示可以运行mysql_secure_installation.运行mysql_secure_installation会执行几个设置:  a)为root用户设置密码  ...

  10. css基础 -文本溢出 text-overflow:ellipsis;

    .className{overflow:hidden;white-space:nowrap;text-overflow:ellipsis;} 外部结构如下是就失效了:(移动端) <a class ...