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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?xml version="1.0" encoding="UTF-8"?>
<a:service xmlns:a="http://www.leaf-framework.org/application" xmlns:s="leaf.plugin.script" trace="true">
<a:init-procedure>
<s:server-script><![CDATA[
importPackage(Packages.batch_download);
importPackage(Packages.ZipCompress);
//@author zhuxianfei 20180117
//@purpose 实现多个合同附件打包成一个压缩包下载
//打包文件路径,迁移环境需要修改
var file_dir = "E:\hls_files\dr_hls\hls_download";
//下载压缩包
function downLoadZip(filePath, fileName) {
try {
var res = $ctx['_instance.javax.servlet.http.HttpServletResponse'];
var file = new java.io.File(filePath);
res.setCharacterEncoding("UTF-8");
res.setHeader("Content-disposition", "attachment; filename=" + encodeURI(fileName, 'utf-8'));
var len = file.length();
res.setContentLength(len);
res.setContentType("application/zip");
var fis = new java.io.FileInputStream(file);
var buff = new java.io.BufferedInputStream(fis);
var b = new java.lang.reflect.Array.newInstance(java.lang.Byte.TYPE, 1024 * 64);
var k = 0;
var os = res.getOutputStream();
// start download
while (k < len) {
var j = buff.read(b, 0, 1024);
k += j;
os.write(b, 0, j);
}
os.flush();
buff.close();
// file.delete();
} catch (e) {
var logger = $logger("server-script");
logger.severe(e.message)
}
//return null;
}
//将各个合同下的附件复制到指定的目标文件夹
try {
//时间戳作为压缩文件夹名称
var folder_name = "支付表附件下载"+ (new Date()).valueOf();
var con_batch_dl = $bm('cont.CON309.con_batch_download_dll');
var result = con_batch_dl.queryAsMap();
var arr_con = result.getChildren();
for (var j = 0;j < arr_con.length;j++) {
var con = arr_con[j];
var attachment_batch_dl = $bm('cont.CON309.lease_atm_batch_dl');
var arr = attachment_batch_dl.queryAsMap({
table_name: 'CON_CONTRACT',
table_pk_value: con.contract_id,
cdd_list_id: con.cdd_list_id
}).getChildren();
var file_exist_list = {};
for (var i = 0;i < arr.length;i++) {
var f = arr[i];
if (f.file_path && !file_exist_list[f.file_name]) {
var toPath = file_dir + folder_name + '/' + con.contract_number + '/' + f.file_name;
//copy file to folder
BatchDownload.copyFile(f.file_path, toPath);
} else {
file_exist_list[f.file_name] = file_exist_list[f.file_name] * 1 + 1;
var last_index = f.file_name.lastIndexOf(".");
var temp_exists_file_name = f.file_name.substr(0, last_index);
var temp_exists_file_type = f.file_name.substr(last_index, f.file_name.length);
temp_exists_file_name = temp_exists_file_name + '-' + file_exist_list[f.file_name] + temp_exists_file_type;
var toPath = file_dir + folder_name + '/' + con.contract_number + '/' + temp_exists_file_namee;
//copy file to folder
BatchDownload.copyFile(f.file_path, toPath);
}
}
}
} catch (e) {
var logger = $logger("server-script");
logger.severe(e.message)
}
//压缩文件夹
FileCompress.compress(file_dir + folder_name + '/', file_dir + folder_name + ".zip");
//下载文件
downLoadZip(file_dir + folder_name + ".zip", folder_name + ".zip");
]]></s:server-script>
</a:init-procedure>
</a:service>