SpringBoot之文件下载
package org.springboot.controller; import org.springboot.constant.Constant;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder; /**
* @Auther:GongXingRui
* @Date:2018/12/24
* @Description:
**/
@Controller
public class FileDownload { //实现Spring Boot 的文件下载功能,映射网址为/download
@RequestMapping("/download")
public String downloadFile(HttpServletRequest request,
HttpServletResponse response) throws UnsupportedEncodingException {
// String picPath = "C:/Temp/pic";
String picPath = Constant.CONFIG_PROPERTIES.getProperty("download.path"); // 获取指定目录下的第一个文件
File scFileDir = new File(picPath);
File TrxFiles[] = scFileDir.listFiles();
System.out.println(TrxFiles[0]);
String fileName = TrxFiles[0].getName(); //下载的文件名 // 如果文件名不为空,则进行下载
if (fileName != null) {
//设置文件路径
File file = new File(picPath, 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 file successfully!");
}
catch (Exception e) {
System.out.println("Download the file 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;
} }
SpringBoot之文件下载的更多相关文章
- SpringBoot的文件下载
SpringBoot的文件下载 2017年11月29日 10:32:20 阅读数:3907 SpringBoot的文件下载方法有很多,此处只记录使用Spring的Resource实现类FileSyst ...
- SpringBoot/SpringMVC文件下载方式
本篇文章引用外网博客代码,共描述SpringMVC下三种文件下载方式,本人测试在SpringBoot(2.0以上版本)正常使用. 引用博客,强烈推荐https://www.boraji.com. pa ...
- 用Springboot实现文件下载功能
ApiOperation(value = "下载文件", httpMethod = "GET", notes = "downloadFile" ...
- SpringBoot入门一:基础知识(环境搭建、注解说明、创建对象方法、注入方式、集成jsp/Thymeleaf、logback日志、全局热部署、文件上传/下载、拦截器、自动配置原理等)
SpringBoot设计目的是用来简化Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置.通过这种方式,SpringBoot致力于在蓬勃发 ...
- vue下载文件
import fileDownload from 'js-file-download' let params = { ", ", "filename":&quo ...
- SpringBoot(三):文件下载
SpringBoot(三):文件下载 2017年08月02日 10:46:42 阅读数:6882 在原来的SpringBoot–uploadfile项目基础上添加文件下载的Controller: @R ...
- Springboot文件下载
https://blog.csdn.net/stubbornness1219/article/details/72356632 Springboot对资源的描述提供了相应的接口,其主要实现类有Clas ...
- springBoot中使用使用junit测试文件上传,以及文件下载接口编写
本篇文章将介绍如何使junit在springBoot中测试文件的上传,首先先阅读如何在springBoot中进行接口测试. 文件上传操作测试代码 import org.junit.Before; im ...
- 对Web(Springboot + Vue)实现文件下载功能的改进
此为 软件开发与创新 课程的作业 对已有项目(非本人)阅读分析 找出软件尚存缺陷 改进其软件做二次开发 整理成一份博客 原项目简介 本篇博客所分析的项目来自于 ジ绯色月下ぎ--vue+axios+sp ...
随机推荐
- syzkaller 资料集合
搭建 1.Setup: Ubuntu host, QEMU vm, x86-64 kernel https://github.com/google/syzkaller/blob/master/docs ...
- sk_buff Structure
The structure has changed many times in the history of the kernel,both to add new options and to reo ...
- bootstraptable 分页查询
1.前端配置 2.后台输出格式化数据 1.前端配置 @{ Layout = null; } <!DOCTYPE html> <html> <head> <me ...
- Java8之lambda表达式
一.什么是lambda表达式? Lambda 是一个匿名函数,我们可以把 Lambda 表达式理解为是一段可以传递的代码(将代码像数据一样进行传递).可以写出更简洁.更灵活的代码.作为一种更紧凑的代码 ...
- docker+openvswitch实现主机与容器的网络通信
主要用到openvswitch和netns网络名称空间的相关知识还有ip命令的使用. 实验环境的结构图如下: 思路如下: 安装openvswitch ovs创建br0,br1,并启动两个不加载网络的d ...
- face recognition[variations of softmax][L-Softmax]
本文来自<Large-Margin Softmax Loss for Convolutional Neural Networks>,时间线为2016年12月,是北大和CMU的作品. 0 引 ...
- 常用的一些markdown格式
现在工作中,大家都习惯使用markdown写文档.按照其语法来,直接出样式,确实很方便. mac上我使用了macdown工具,只要支持markdown的差不多都一样. 自己经常习惯用的几个语法规则,记 ...
- Web组件流畅拖动效果
拖动效果,可以形象的帮助用户处理一些问题,比如Windows删除文件,只需将文件拖动至回收站即可.比起右键显得更形象,我觉得更好玩一点^_^.当然,在其他许多方面,其实也有用到拖动效果,只是他们不是那 ...
- 【vue】vue-router跳转路径url多种格式
1.形如 http://localhost:8080/#/book?id=**** ①路由配置 ②路由定向链接,以query传参id 另外,获取query传递的参数id用 this.$route. ...
- 【原创】分布式之elk日志架构的演进
引言 好久没写分布式系列的文章了,最近刚好有个朋友给我留言,想看这方面的知识.其实这方面的知识,网上各种技术峰会的资料一抓一大把.博主也是凑合着写写.感觉自己也写不出什么新意,大家也凑合看看. 日志系 ...