Spring Boot 文件下载
1. 文件下载类
import javax.servlet.http.HttpServletResponse;
import java.io.*; public class DownloadUtil { public boolean fileDownload(HttpServletResponse response,String filePath,String fileName)
throws UnsupportedEncodingException { File file = new File(filePath);//创建下载的目标文件,参数为文件的真实路径
if(file.exists()){ //判断文件是否存在
response.setContentType("application/vnd.ms-excel;charset=UTF-8");
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Disposition", "attachment;fileName=" + java.net.URLEncoder.encode(fileName,"UTF-8"));
byte[] buffer = new byte[1024];
FileInputStream fis = null; //文件输入流
BufferedInputStream bis = null; OutputStream os = null; //输出流
try {
os = response.getOutputStream();
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
int i = bis.read(buffer);
while(i != -1){
os.write(buffer);
i = bis.read(buffer);
} } catch (Exception e) {
e.printStackTrace();
return false;
}
try {
bis.close();
fis.close();
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
return true;
}
2. 文件下载a标签
<a th:href="@{/download/} + ${fileName}+'?filePath='+${filePath}">点击下载文件</a>
filePath为文件的真实路径,由于路径的符号与RESTful API风格冲突,所以采取传统a标签的传参方式
3. 控制器
/**
* 测试文件下载
* @param fileName
* @param filePath
* @param response
* @return
* @throws UnsupportedEncodingException
*/
@GetMapping("/download/{fileName}")
public String testDownload(@PathVariable("fileName") String fileName,String filePath,HttpServletResponse response) throws UnsupportedEncodingException { System.out.println("==>开始文件下载!"); DownloadUtil downloadUtil = new DownloadUtil(); downloadUtil.fileDownload(response,filePath,fileName); return null;
}
Spring Boot 文件下载的更多相关文章
- Spring框架学习笔记(7)——Spring Boot 实现上传和下载
最近忙着都没时间写博客了,做了个项目,实现了下载功能,没用到上传,写这篇文章也是顺便参考学习了如何实现上传,上传和下载做一篇笔记吧 下载 主要有下面的两种方式: 通过ResponseEntity实现 ...
- Spring Boot实现文件下载功能
我们只需要创建一个控制器(Controler)文件,即Controller目录下的File_Download.java,其完整目录如下: @Controller public class File_D ...
- Spring Boot入门(11)实现文件下载功能
在这篇博客中,我们将展示如何在Spring Boot中实现文件的下载功能. 还是遵循笔者写博客的一贯风格,简单又不失详细,实用又能让你学会. 本次建立的Spring Boot项目的主要功能 ...
- spring boot 实现文件下载
html 代码 js部分 window.location.href= this.Baseurl+'/plan/down?file='+filename; spring boot 后台代码@GetMap ...
- [Spring boot] web应用返回jsp页面
同事创建了一个spring boot项目,上传到svn.需要我来写个页面.下载下来后,始终无法实现在Controller方法中配置直接返回jsp页面. 郁闷了一下午,终于搞定了问题.在此记录一下. 目 ...
- spring boot / cloud (八) 使用RestTemplate来构建远程调用服务
spring boot / cloud (八) 使用RestTemplate来构建远程调用服务 前言 上周因家里突发急事,请假一周,故博客没有正常更新 RestTemplate介绍: RestTemp ...
- java代码自动下载Spring Boot用户手册
本示例演示Spring Boot 1.5.9.RELEASE版本的用户手册下载 pom.xml <?xml version="1.0" encoding="UTF- ...
- Spring Boot 单元测试详解+实战教程
Spring Boot 的测试类库 Spring Boot 提供了许多实用工具和注解来帮助测试应用程序,主要包括以下两个模块. spring-boot-test:支持测试的核心内容. spring-b ...
- Spring Boot 文件上传与下载
原文地址: https://www.cnblogs.com/studyDetail/p/7003253.html 1.在pom.xml文件中添加依赖 <project xmlns="h ...
随机推荐
- ebay上传图片的要求
eBay's Picture Requirements Introduction to Pictures in Listings Pictures make an item more appealin ...
- Mysql数据库命令行输入错误怎么办
Mysql数据库命令行输入错误 缺少另一半 错误输入 ' 或 " 时,必须键入另一半才能退出命令. 缺少分号 写入语句缺少分号需要补全. 输入quit或者exit退出 ctrl+c,完全退出 ...
- Python的一些列表方法
1.append:方法append用于将一个对象附加到列表末尾,直接修改列表 lst=[1,2,3,4] lst.append(5) print(lst) 1,2,3,4,5 2.clear:方法cl ...
- mysql的索引方法btree和hash的区别
原文链接: http://www.91w.net/database/330.html 1. Hash索引: Hash 索引结构的特殊性,其检索效率非常高,索引的检索可以一次定位,不像B-Tree 索引 ...
- ArcGis EsriAddin加载项的安装路径与程序启动路径
安装路径: 在C:\Users\用户名\Documents\ArcGIS\AddIns\Desktop版本号\{…………一组GUID…………}这样的路径下. 例:C:\Users\Adminis ...
- ARM 寄存器 和 工作模式了解
一. ARM 工作模式 1. ARM7,ARM9,ARM11,处理器有 7 种工作模式:Cortex-A 多了一个监视模式(Monitor) 2. 用户模式:非特权模式,大部分任务执行在这种模式 ...
- shell command to replace UltraEdit
cat abc.txt |hexdump -C cat abc.txt |hexdump -C
- 深度探索C++对象模型之第三章:数据语义学
如下三个类: class X { }: class Y :public virtual X { }; class Z : public virtual X {}; class A :public Y, ...
- margin与padding
1.不加内边距的div: <div style="width:150px; height:150px; "> <div style="width: ...
- 关于synchronized和Lock
原文链接:关于volatile关键字解析,synchronized和Lock参考 深入浅出,解释的非常清楚,有条理~~~ 以下为转载内容: Java并发编程:volatile关键字解析 volatil ...