/** * 下载对账账单明细文件 * * @param request * @param response * @return * @throws Exception */ public ModelAndView downloadCheckBill(H_zip headers not found. pr">
赞
踩
下载文件时直接对流进行zip加密压缩
1、使用form提交表单,请求下载.do
2、使用zip4j_1.3.1.jar
- <span style="white-space:pre"> </span>/**
- * 下载对账账单明细文件
- *
- * @param request
- * @param response
- * @return
- * @throws Exception
- */
- public ModelAndView downloadCheckBill(HttpServletRequest request,
- HttpServletResponse response) throws Exception {
-
- try {
- EntCurrentUser entCurrentUser = new EntCurrentUser(request);
- EntUser entUser = entCurrentUser.getEntCurrentUser();
-
- String batchNo = request.getParameter("batchNo_form");
- String filePath = request.getParameter("filePath_form");
- String pwd = request.getParameter("pwd_form");
-
- String filePathRoot = PropertiesUtil.getResourceString("cloud.checkbill_file_path");
- String filePathAll = filePathRoot + filePath;
-
- Map<String,Object> paramMap = new HashMap<String,Object>();
- paramMap.put("userName", entUser.getUserName());
- paramMap.put("batchNo", batchNo);
- paramMap.put("filePath", filePath);
- logger.info("ajax下载-对账账单明细文件,请求参数:paramMap=" + paramMap);
-
- String fileName = filePathAll.substring(filePathAll.lastIndexOf("/")+1);
- String zipFileName = fileName+".zip";
-
- //获取输出到页面的流
- response.setContentType("application/x-msdownload;charset=utf-8");
- response.addHeader("Content-Disposition", "attachment;filename=" + zipFileName);
- response.setBufferSize(81920);
- OutputStream os = response.getOutputStream();
-
- //将nas文件读取到流中
- FileInputStream fis = new FileInputStream(filePathAll);
- ByteArrayOutputStream bos2 = new ByteArrayOutputStream();
- byte[] b = new byte[1024];
- int n;
- int off = 0;
- while ((n = fis.read(b)) != -1) {
- bos2.write(b, off, n);
- }
- fis.close();
-
- ZipOutputStream outputStream = new ZipOutputStream(os,new ZipModel());
- ZipParameters parameters = new ZipParameters();
- parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
- parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
- parameters.setSourceExternalStream(true);
- parameters.setFileNameInZip(fileName);
- parameters.setEncryptFiles(true);
- parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_STANDARD);
- parameters.setPassword(pwd);
-
- outputStream.putNextEntry(null, parameters);
- outputStream.write(bos2.toByteArray());
-
- bos2.flush();
- bos2.close();
-
- outputStream.flush();
- outputStream.closeEntry();
- outputStream.finish();
- outputStream.close();
-
- os.flush();
- logger.info("ajax下载-对账账单明细文件成功");
- } catch (Exception e){
- logger.info("下载过程失败出现异常",e);
- }
- return null;
- }

1、参考文章1内容
JQuery的ajax函数的返回类型只有xml、text、json、html等类型,没有“流”类型,所以我们要实现ajax下载,不能够使用相应的ajax函数进行文件下载。但可以用js生成一个form,用这个form提交参数,并返回“流”类型的数据。在实现过程中,页面也没有进行刷新。
var form=$("<form>");//定义一个form表单
form.attr("style","display:none");
form.attr("target","");
form.attr("method","post");
form.attr("action","exportData");
var input1=$("<input>");
input1.attr("type","hidden");
input1.attr("name","exportData");
input1.attr("value",(new Date()).getMilliseconds());
$("body").append(form);//将表单放置在web中
form.append(input1);
form.submit();//表单提交
2、参考文章2内容
最近公司准备让各项目组提供公共工具组件,手中正好无事便研究其中一个工具 - 文件压缩与解压缩工具。
目前JAVA API已提供对于ZIP文件的压缩与解压缩,但网上总结不支持ZIP文件加密与解密甚至对于中文支持也有问题,于是果断找其他的支持加密解密的第三方包。 winzipaes 与 ZIP4J 都符合项目的要求 ,最终选择ZIP4J来进行使用。
ZIP4J 项目地址为 :http://www.lingala.net/zip4j/ ,但该地址无法直接访问需要使用代理进行访问
再提供一个在线代理网站:http://www.7daili.com/
目前ZIP4J 版本为:1.3.1 ,只需要直接下一载一个zip4j_1.3.1.jar一个jar包即可,同时可以下载官网提供的例子进行学习,例子相当详细(官网提供的例子的jdk 为1.4的版本 导入后需要修改一下)
官网提供的例子程序比较简单与小巧 整个程序也就一百多k,但demo程序对于各种功能的使用讲的非常详细。
项目包结构分为三层:
示例代码:
示例代码中需要对文件进行操作,为方便程序运行创建以下三个文件 可以直接运行大多数代码
示例1 创建压缩包添 加文件到压缩包中(未设置加密)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
public
class
AddFilesDeflateComp {
public
AddFilesDeflateComp() {
try
{
ZipFile zipFile =
new
ZipFile(
"c:\\ZipTest\\AddFilesDeflateComp.zip"
);
ArrayList<File> filesToAdd =
new
ArrayList<File>();
filesToAdd.add(
new
File(
"c:\\ZipTest\\sample.txt"
));
filesToAdd.add(
new
File(
"c:\\ZipTest\\myvideo.avi"
));
filesToAdd.add(
new
File(
"c:\\ZipTest\\mysong.mp3"
));
ZipParameters parameters =
new
ZipParameters();
parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
// set compression method to deflate compression
parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
zipFile.addFiles(filesToAdd, parameters);
}
catch
(ZipException e) {
e.printStackTrace();
}
}
/**
* @param args
*/
public
static
void
main(String[] args) {
new
AddFilesDeflateComp();
}
}
|
示例2 创建压缩包添加文件到 指定目录中进行压缩
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
public
class
AddFilesToFolderInZip {
public
AddFilesToFolderInZip() {
try
{
ZipFile zipFile =
new
ZipFile(
"c:\\ZipTest\\AddFilesDeflateComp.zip"
);
ArrayList<File> filesToAdd =
new
ArrayList<File>();
filesToAdd.add(
new
File(
"c:\\ZipTest\\sample.txt"
));
filesToAdd.add(
new
File(
"c:\\ZipTest\\myvideo.avi"
));
filesToAdd.add(
new
File(
"c:\\ZipTest\\mysong.mp3"
));
ZipParameters parameters =
new
ZipParameters();
parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
parameters.setRootFolderInZip(
"test2/"
);
zipFile.addFiles(filesToAdd, parameters);
}
catch
(ZipException e) {
e.printStackTrace();
}
}
/**
* @param args
*/
public
static
void
main(String[] args) {
new
AddFilesToFolderInZip();
}
}
|
示例3 添加文件夹到压缩包中
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
public
class
AddFolder {
public
AddFolder() {
try
{
ZipFile zipFile =
new
ZipFile(
"c:\\ZipTest\\AddFolder.zip"
);
String folderToAdd =
"c:\\FolderToAdd"
;
ZipParameters parameters =
new
ZipParameters();
parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
zipFile.addFolder(folderToAdd, parameters);
}
catch
(ZipException e) {
e.printStackTrace();
}
}
public
static
void
main(String[] args) {
new
AddFolder();
}
}
|
示例4 创建加密压缩包
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
public
class
AddFilesWithAESEncryption {
public
AddFilesWithAESEncryption() {
try
{
ZipFile zipFile =
new
ZipFile(
"c:\\ZipTest\\AddFilesWithAESZipEncryption.zip"
);
ArrayList<File> filesToAdd =
new
ArrayList<File>();
filesToAdd.add(
new
File(
"c:\\ZipTest\\sample.txt"
));
filesToAdd.add(
new
File(
"c:\\ZipTest\\myvideo.avi"
));
filesToAdd.add(
new
File(
"c:\\ZipTest\\mysong.mp3"
));
ZipParameters parameters =
new
ZipParameters();
parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
parameters.setEncryptFiles(
true
);
parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_AES);
parameters.setAesKeyStrength(Zip4jConstants.AES_STRENGTH_256);
parameters.setPassword(
"123"
);
zipFile.addFiles(filesToAdd, parameters);
}
catch
(ZipException e) {
e.printStackTrace();
}
}
public
static
void
main(String[] args) {
new
AddFilesWithAESEncryption();
}
}
|
加密压缩包后打开时需要输入密码,与rar相同:
示例5 创建分卷压缩包
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
public
class
CreateSplitZipFile {
public
CreateSplitZipFile() {
try
{
ZipFile zipFile =
new
ZipFile(
"c:\\ZipTest\\CreateSplitZipFile.zip"
);
ArrayList<File> filesToAdd =
new
ArrayList<File>();
filesToAdd.add(
new
File(
"c:\\ZipTest\\sample.txt"
));
filesToAdd.add(
new
File(
"c:\\ZipTest\\myvideo.avi"
));
filesToAdd.add(
new
File(
"c:\\ZipTest\\mysong.mp3"
));
ZipParameters parameters =
new
ZipParameters();
parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
zipFile.createZipFile(filesToAdd, parameters,
true
,
65536
);
}
catch
(ZipException e) {
e.printStackTrace();
}
}
/**
* @param args
*/
public
static
void
main(String[] args) {
new
CreateSplitZipFile();
}
}
|
示例6 通过流的方式添加文件到压缩包中
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
public
class
AddStreamToZip {
public
AddStreamToZip() {
InputStream is =
null
;
try
{
ZipFile zipFile =
new
ZipFile(
"c:\\ZipTest\\AddStreamToZip.zip"
);
ZipParameters parameters =
new
ZipParameters();
parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
parameters.setFileNameInZip(
"yourfilename.txt"
);
parameters.setSourceExternalStream(
true
);
is =
new
FileInputStream(
"c:\\ZipTest\\sample.txt"
);
zipFile.addStream(is, parameters);
}
catch
(Exception e) {
e.printStackTrace();
}
finally
{
if
(is !=
null
) {
try
{
is.close();
}
catch
(IOException e) {
e.printStackTrace();
}
}
}
}
public
static
void
main(String[] args) {
new
AddStreamToZip();
}
}
|
示例7 解压压缩文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
public
class
ExtractAllFiles {
public
ExtractAllFiles() {
try
{
ZipFile zipFile =
new
ZipFile(
"c:\\ZipTest\\ProgressInformation.zip"
);
zipFile.extractAll(
"c:\\ZipTest1"
);
}
catch
(ZipException e) {
e.printStackTrace();
}
}
/**
* @param args
*/
public
static
void
main(String[] args) {
new
ExtractAllFiles();
}
}
|
1
2
3
4
|
if
(zipFile.isEncrypted()) {
// if yes, then set the password for the zip file
zipFile.setPassword(
"test123!"
);
}
|
压缩效率
ZIP4J提供5中压缩算法:
根据API提供的几种不同压缩级别进行测试(文件夹压缩),测试结果如下:
一个180M的文件夹压缩后
与WinRAR 相比较来说,压缩时间还是很不错 但对文件的压缩大小来说还是WINRAR要强一些 (各人电脑配置不同 压缩的时间这些也不同)
以上便是ZIP4J提供的一些常用的工具方法,总结在此,方便自己与大家使用。如果有更好的工具或者有不正确的地方欢迎大家指出!
参考文章1:http://www.cnblogs.com/sydeveloper/archive/2013/05/14/3078295.html AJAX实现文件下载
参考文章2:http://blog.csdn.net/educast/article/details/38755287 ZIP文件压缩与解压缩学习
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。