springMVC实现基本文件夹压缩下载功能
将文件夹压缩后下载:
@Slf4j
public class Test { private static final String BASE_PATH = "/root/doc/"; private static final String TEMP_PATH = "/root/temp/"; private static final String FILE_NAME = "测试"; @RequestMapping(value = "download", method = RequestMethod.GET, produces = {MediaType.APPLICATION_JSON_VALUE})
public ResponseEntity<byte[]> download() throws Exception {
fileToZip(BASE_PATH, TEMP_PATH, FILE_NAME);
File file = new File(TEMP_PATH + "//" + FILE_NAME + ".zip");
HttpHeaders headers = new HttpHeaders();
// 为了解决中文名称乱码问题
String fileName = new String(FILE_NAME.getBytes("UTF-8"), "iso-8859-1");
headers.setContentDispositionFormData("attachment", fileName + ".zip");
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
return new ResponseEntity<>(FileUtils.readFileToByteArray(file), headers, HttpStatus.CREATED);
} private static void fileToZip(String sourceFilePath, String zipFilePath, String fileName) throws Exception {
File sourceFile = new File(sourceFilePath);
if (!sourceFile.exists()) {
log.info("{} >>>> is not exists", sourceFilePath);
throw new IotBusinessException(ExceptionDefinition.FILE_PATH_NOT_EXISTS.code, ExceptionDefinition.FILE_PATH_NOT_EXISTS.key, "filePathNotExists");
}
File file = new File(zipFilePath);
if (!file.exists()) {
file.mkdirs();
}
File zipFile = new File(zipFilePath + "/" + fileName + ".zip");
if (zipFile.exists()) {
// zip文件存在, 将原有文件删除
zipFile.delete();
}
// zip文件不存在, 打包
pushZip(sourceFile, sourceFilePath, zipFile);
} private static void pushZip(File sourceFile, String sourceFilePath, File zipFile) throws Exception {
FileInputStream fis;
BufferedInputStream bis = null;
FileOutputStream fos;
ZipOutputStream zos = null;
try {
File[] sourceFiles = sourceFile.listFiles();
if (null == sourceFiles || sourceFiles.length < 1) {
log.info("{} >>>> is null", sourceFilePath);
} else {
fos = new FileOutputStream(zipFile);
zos = new ZipOutputStream(new BufferedOutputStream(fos));
byte[] bytes = new byte[1024 * 10];
for (int i = 0; i < sourceFiles.length; i++) {
// 创建ZIP实体,并添加进压缩包
ZipEntry zipEntry = new ZipEntry(sourceFiles[i].getName());
zos.putNextEntry(zipEntry);
// 读取待压缩的文件并写进压缩包里
fis = new FileInputStream(sourceFiles[i]);
bis = new BufferedInputStream(fis, 10240);
int read = 0;
while ((read = bis.read(bytes, 0, 10240)) != -1) {
zos.write(bytes, 0, read);
}
}
}
} catch (IOException e) {
e.printStackTrace();
throw e;
} finally {
try {
if (null != bis) {
bis.close();
}
if (null != zos) {
zos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
springMVC实现基本文件夹压缩下载功能的更多相关文章
- 【C#公共帮助类】WinRarHelper帮助类,实现文件或文件夹压缩和解压,实战干货
关于本文档的说明 本文档使用WinRAR方式来进行简单的压缩和解压动作,纯干货,实际项目这种压缩方式用的少一点,一般我会使用第三方的压缩dll来实现,就如同我上一个压缩类博客,压缩的是zip文件htt ...
- JavaWeb实现文件上传下载功能实例解析
转:http://www.cnblogs.com/xdp-gacl/p/4200090.html JavaWeb实现文件上传下载功能实例解析 在Web应用系统开发中,文件上传和下载功能是非常常用的功能 ...
- SharpCompress的压缩文件解压和文件夹压缩
1.前言 最近做一个功能需要用到对压缩文件的解压,就找到了这个SharpCompress不错,还能解压rar的文件.但是网上的资料和我拿到的SharpCompress.dll的方法有些出入,所以我就自 ...
- JavaWeb实现文件上传下载功能实例解析 (好用)
转: JavaWeb实现文件上传下载功能实例解析 转:http://www.cnblogs.com/xdp-gacl/p/4200090.html JavaWeb实现文件上传下载功能实例解析 在Web ...
- linux文件压缩与文件夹压缩(打包)
目录 一:linux文件压缩 1.linux常见的压缩包有哪些? 2.bzip压缩(文件) 二:打包(文件夹压缩) 1.打包命令 2.参数 3.参数解析(实战) 4.注意事项 简介: win中的压缩包 ...
- SharpZipLib 文件/文件夹压缩
一.ZipFile ZipFile类用于选择文件或文件夹进行压缩生成压缩包. 常用属性: 属性 说明 Count 文件数目(注意是在ComitUpdat之后才有) Password 压缩包密码 Siz ...
- 使用ICSharpZipLib将文件夹压缩为zip文件
序言: 在我接触Git和SVN之前,我最常用的保存数据的办法就是把文件夹压缩成一个zip文件,添加上时间戳.下面是我在学习C#的文件操作之后做的一个练习,使用开源的ICSharpZipLib来 ...
- C# 文件/文件夹压缩
一.ZipFile ZipFile类用于选择文件或文件夹进行压缩生成压缩包. 常用属性: 属性 说明 Count 文件数目(注意是在ComitUpdat之后才有) Password 压缩包密码 Siz ...
- C# 文件/文件夹压缩解压缩
项目上用到的,随手做个记录,哈哈. 直接上代码: using System; using System.Data; using System.Configuration; using System.C ...
随机推荐
- msp430入门编程30
msp430中C语言的文件管理
- windows7 下安装使用memcached(二)
Memcached 安装使用 本地环境:Windows7 64位web环境:wamp集成环境,php版本:PHP Version 7.1.17 学习参考网站: RUNOOB.COM官网 http:/ ...
- HDU 6396 贪心+优先队列+读入挂
Swordsman Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total ...
- PAT (Advanced Level) 1036. Boys vs Girls (25)
简单题. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> ...
- [React] Use the Fragment Short Syntax in Create React App 2.0
create-react-app version 2.0 added a lot of new features. One of the new features is upgrading to Ba ...
- 读书笔记-HBase in Action-第三部分应用-(2)GIS系统
本章介绍用HBase存储.高效查询地理位置信息. Geohash空间索引 考虑LBS应用中常见的两个问题:1)查找离某地近期的k个地点.2)查找某区域内地点. 假设要用HBase实现高效查找,首先要考 ...
- vux tabbar 组件
1.App.vue <!-- 入口文件 --> <template> <div id="app"> <!-- 视图层 --> < ...
- protobuf 之 MessageLite 接口摘录
class LIBPROTOBUF_EXPORT MessageLite { public: inline MessageLite() {} virtual ~MessageLite(); // Ba ...
- JavaScript语言基础12
使用if语句时.假设碰到很多个条件时,就不应该继续使用if语句了,JavaScript提供了一个更高效的替代方案,那就是switch语句,我们先看看switch语句的模板: <HTML> ...
- Fluently NHibernate映射多个实体程序集
Fluently NHibernate有个好处就是可以在代码里定义实体类,而不必写冗长的XML. 但通常,一个项目对应的所有的实体类,都编译成一个DLL.如果有个项目,是在某个父项目的基础上再扩展,那 ...