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 ...
随机推荐
- gcc,gdb基础学习2
gdb调试: 源文件:test.cc >>g++ -g test.cc -o test.o 这里需要参数-g,因为接下来要使用gdb进行调试,生成目标文件test·o 因为需要将调试信息 ...
- WKWebView的了解
1. http://blog.csdn.net/chenyong05314/article/details/53735215 2. http://www.jianshu.com/p/6ba250744 ...
- kafka调试中遇到Connection to node -1 could not be established. Broker may not be available.
https://blog.csdn.net/Mr_Hou2016/article/details/79484032
- poj -1185 炮兵阵地 (经典状压dp)
http://poj.org/problem?id=1185 参考博客:http://poj.org/problem?id=1185 大神博客已经讲的很清楚了,注意存状态的时候是从1开始的,所以初始化 ...
- 洛谷—— P1977 出租车拼车
https://www.luogu.org/problem/show?pid=1977 题目背景 话说小 x 有一次去参加比赛,虽然学校离比赛地点不太远,但小 x 还是想坐 出租车去.大学城的出租车总 ...
- 《springMVC》学习笔记
1.SpringMVC框架 1.1 MVC在b/s系统下的应用 用户发送request请求到Controller Controller请求模型(Model)进行处理 Model将处理结果返回到Cont ...
- 转 gSOAP中使用TCP协议传输数据
一 模型 TCP/IP是一个协议族(Internet protocol suite),包含众多的协议,传输控制协议(TCP)和网际协议(IP)分属不同的层次,是保证数据完整传输的两个基本的重要协议. ...
- ORA-01034: ORACLE not available 出错
调用db.rlogon("sm/sm")出现以下错误 ORA-01034: ORACLE not availableORA-27101: shared memory realm d ...
- 【Nginx】发送响应
请求处理完毕后,需要向用户发送http响应,告知客户端Nginx的执行结果.http响应主要包括响应行.响应头部.包体三部分.发送http响应时需要执行发送http头部(发送http头部时也会发送响应 ...
- History(历史)命令用法 15 例
如果你经常使用 Linux 命令行,那么使用 history(历史)命令可以有效地提升你的效率.本文将通过实例的方式向你介绍 history 命令的 15 个用法. 使用 HISTTIMEFORMAT ...