Spring Boot实现文件下载功能
我们只需要创建一个控制器(Controler)文件,即Controller目录下的File_Download.java,其完整目录如下:
@Controller
public class File_Download {
//实现Spring Boot 的文件下载功能,映射网址为/download
@RequestMapping("/download")
public String downloadFile(HttpServletRequest request,
HttpServletResponse response) throws UnsupportedEncodingException {
// 获取指定目录下的第一个文件
File scFileDir = new File("E://music_eg");
File TrxFiles[] = scFileDir.listFiles();
System.out.println(TrxFiles[0]);
String fileName = TrxFiles[0].getName(); //下载的文件名
// 如果文件名不为空,则进行下载
if (fileName != null) {
//设置文件路径
String realPath = "E://music_eg/";
File file = new File(realPath, fileName);
// 如果文件名存在,则进行下载
if (file.exists()) {
// 配置文件下载
response.setHeader("content-type", "application/octet-stream");
response.setContentType("application/octet-stream");
// 下载文件能正常显示中文
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
// 实现文件下载
byte[] buffer = new byte[1024];
FileInputStream fis = null;
BufferedInputStream bis = null;
try {
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
OutputStream os = response.getOutputStream();
int i = bis.read(buffer);
while (i != -1) {
os.write(buffer, 0, i);
i = bis.read(buffer);
}
System.out.println("Download the song successfully!");
}
catch (Exception e) {
System.out.println("Download the song failed!");
}
finally {
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
return null;
}
}
这样我们就完成了Spring Boot的文件下载功能。
运行Spring Boot项目后,在浏览器中输入:http://localhost:8080/download , 你会发现什么?那就是你的浏览器已经开始下载E盘music_eg目录下的某一个文件啦(前提是E盘中存在music_eg目录,当然里面还得有文件,本例仅作为测试),如下图所示:
Spring Boot实现文件下载功能的更多相关文章
- [Spring Boot 系列] 集成maven和Spring boot的profile功能
由于项目的需要, 今天给spirng boot项目添加了profile功能.再网上搜索了一圈,也没有找到满意的参考资料,其实配置并不难,就是没有一个one stop(一站式)讲解的地方,所以有了写这篇 ...
- 004-集成maven和Spring boot的profile功能打包
参考地址:https://blog.csdn.net/lihe2008125/article/details/50443491 一.主要目标 1.通过mvn在命令行中打包时,可以指定相应的profil ...
- 集成maven和Spring boot的profile功能
思路:maven支持profile功能,当使用maven profile打包时,可以打包指定目录和指定文件,且可以修改文件中的变量.spring boot也支持profile功能,只要在applica ...
- Spring boot 的profile功能如何实现多环境配置自动切换
通常服务端应用开发需要经过以下几个流程: 开发 -> 测试 -> RC验证 -> 上线 这就涉及到四个不同的环境,开发环境.测试环境.RC环境以及生产环境,为了避免不同环境之间相互干 ...
- spring boot 实现文件下载
html 代码 js部分 window.location.href= this.Baseurl+'/plan/down?file='+filename; spring boot 后台代码@GetMap ...
- 041 Spring Boot中排除功能的处理
这个问题,原本是没有注意,主要是理解的不够深刻. 1.先看我的配置文件 package com.springBoot.ioc.config; import com.springBoot.ioc.con ...
- Spring Boot入门(11)实现文件下载功能
在这篇博客中,我们将展示如何在Spring Boot中实现文件的下载功能. 还是遵循笔者写博客的一贯风格,简单又不失详细,实用又能让你学会. 本次建立的Spring Boot项目的主要功能 ...
- Spring Boot 单元测试详解+实战教程
Spring Boot 的测试类库 Spring Boot 提供了许多实用工具和注解来帮助测试应用程序,主要包括以下两个模块. spring-boot-test:支持测试的核心内容. spring-b ...
- 值得使用的Spring Boot
2013年12月12日,Spring发布了4.0版本.这个本来只是作为Java平台上的控制反转容器的库,经过将近10年的发展已经成为了一个巨无霸产品.不过其依靠良好的分层设计,每个功能模块都能保持较好 ...
随机推荐
- springAop注解式Demo
package AnnoAspect.Aspect; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.anno ...
- Python读取和写入Excel文件
制作Excel表 常用方法说明 Workbook类 Workbook类创建一个XlswWrite的Workbook对象,相当于创建一个excel表 And_worksheet()用来创建工作表,默认为 ...
- cookie 就是一些字符串信息
什么是 Cookie “cookie 是存储于访问者的计算机中的变量.每当同一台计算机通过浏览器请求某个页面时,就会发送这个 cookie.你可以使用JavaScript 来创建和取回cookie 的 ...
- 动画讲解TCP的3次握手,4次挥手
https://mp.weixin.qq.com/s/TUBhH_lJe6M4KgAZO-rP2A TCP三次握手和四次挥手的问题在面试中是最为常见的考点之一.很多读者都知道三次和四次,但是如果问深入 ...
- git 与 ftp 共同工作
因git主要用于版本管理,代码同步方面,因临时调试等原因,需要使用ftp上传文件. 但因为git的账户为ubuntu,ftp是虚拟账户overlord 导致文件权限不同,出现的问题主要有: 1.ftp ...
- 18.22 sprintf函数功能
函数功能:把格式化的数据写入某个字符串 函数原型:int sprintf( char *buffer, const char *format [, argument] … ); 返回值:字符串长度(s ...
- angularjs 学习小结
1.过滤器的使用 <!DOCTYPE html> <html> <head> <meta charset="{CHARSET}"> ...
- SoapUI测试WebService接口
Getting Started Getting started with some ad-hoc testing of a SOAP service is straight forward; se ...
- 时间复杂度O()与KMP算法
要得到某个结果,可以有很多种方式,算法就是为了寻找一条最快的方式. 而评判其好坏的标准就是时间复杂度. O(1): 我们把执行一次的时间复杂度定义为O(1) sum = a +b; cout < ...
- USD词汇表(USD Glossary)
这篇文章是在学习USD的过程中龟速写成的,目的是将USD的核心设计.相关概念的说明.以及配套API整理出来,为后续进行的USD开发工作提供中文资料支持. 实际上也只有充分理解了USD设计中的每一个知识 ...