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 ...
随机推荐
- oc温习八:static、extern、const 的了解
参考文章:http://www.cocoachina.com/ios/20161110/18035.html 1.const 这个单词翻译成中文是“常量”的意思.在程序中我们知道“常量”的值是不能变的 ...
- 利用百度地图Android sdk高仿微信发送位置功能
接触了百度地图开发平台半个月了,这2天试着模仿了微信给好友发送位置功能,对百度地图的操作能力又上了一个台阶 (假设须要完整demo.请评论留下邮箱) (眼下源代码已经不发送,假设须要源代码.加qq31 ...
- Centos5.11 //IP/phpmyadmin 远程无法登入
异地登入phpmyadmin时,会出现"You don't have permission to access /phpmyadmin/ on this server."这是因为配 ...
- [Unit Testing] Unit Test a Function that Invokes a Callback with a Sinon Spy
Unit testing functions that invoke callbacks can require a lot of setup code. Using sinon.spy to cre ...
- HDU 5301 Buildings(2015多校第二场)
Buildings Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Tota ...
- hdu 3549 Flow Problem(最大流模板题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3549 Problem Description Network flow is a well-known ...
- 杭电1232畅通project
畅通project Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...
- 奥斯卡·王尔德十大经典语录
10不够真诚是危急的,太真诚则绝对是致命的.--摘自<身为艺术家的评论者> "A little sincerity is a dangerous thing, and a gre ...
- Highcharts:X轴分组堆叠图
在设计一个项目中的数据展示页面时.想要设计双X轴,一个轴显示须要的项.一个轴对这些项进行分组.效果如图: Highcharts自带双X轴展示方式.可是效果不是太理想.调整起来也会麻烦些 看到Highc ...
- Linux - Ubuntu中文输入法安装(Ubuntu 12.04)
Ubuntu中文输入法安装(Ubuntu 12.04) 本文地址:http://blog.csdn.net/caroline_wendy Ubuntu作为Linux常见的操作系统,是须要熟练使用的. ...