PreDownload.jsp
3.23 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<%@ page language="java" contentType="application/x-download" pageEncoding="UTF-8"%>
<%@page import="net.sf.json.*"%>
<%@ page import="java.util.*" %>
<%@ page import="java.util.zip.*" %>
<%@ page import="java.security.*" %>
<%@ page import="java.io.*" %>
<%@ page import="weaver.general.*" %>
<%@ page import="weaver.file.*" %>
<%@ page import="weaver.hrm.*" %>
<%@ page import="weaver.conn.*" %>
<jsp:useBean id="ps" class="weaver.mobile.plugin.ecology.service.PluginServiceImpl" scope="page" />
<%!
public String getHash(byte[] content, String hashType) throws Exception {
InputStream fis = new ByteArrayInputStream(content);
byte[] buffer = new byte[1024];
MessageDigest md5 = MessageDigest.getInstance(hashType);
int numRead = 0;
while ((numRead = fis.read(buffer)) > 0) {
md5.update(buffer, 0, numRead);
}
fis.close();
return toHexString(md5.digest());
}
public char[] hexChar = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
public String toHexString(byte[] b) {
StringBuilder sb = new StringBuilder(b.length * 2);
for (int i = 0; i < b.length; i++) {
sb.append(hexChar[(b[i] & 0xf0) >>> 4]);
sb.append(hexChar[b[i] & 0x0f]);
}
return sb.toString();
}
%>
<%
String url = Util.null2String(request.getParameter("url"));
String sessionkey = Util.null2String(request.getParameter("sessionkey"));
if(ps.verify(sessionkey)) {
String filepath = "";
String iszip = "";
String filename = "";
String isaesencrypt="";
String aescode = "";
String hashcode = "";
if(Util.getIntValue(url)>0) {
RecordSet rs = new RecordSet();
String sql = "select isaesencrypt,aescode,imagefilename,imagefiletype,filerealpath,iszip from imagefile where imagefileid = ?";
rs.executeQuery(sql,url);
if(rs.next()) {
filepath = rs.getString("filerealpath");
iszip = rs.getString("iszip");
filename = rs.getString("imagefilename");
isaesencrypt = rs.getString("isaesencrypt");
aescode = rs.getString("aescode");
}
} else {
filepath = request.getRealPath(url);
iszip = "0";
filename = filepath.substring(filepath.lastIndexOf("/")+1);
if(filename.indexOf("../") > 0 || filename.indexOf("\0")>0){
return;
}
}
File file = new File(filepath);
byte[] content = null;
if(file.exists()) {
InputStream is = null;
try {
if (Util.getIntValue(iszip) > 0) {
ZipInputStream zin = new ZipInputStream(new FileInputStream(file));
if (zin.getNextEntry() != null)
is = new BufferedInputStream(zin);
} else {
is = new BufferedInputStream(new FileInputStream(file));
}
if(isaesencrypt.equals("1")){
is = AESCoder.decrypt(is,aescode);
}
byte[] rbs = new byte[2048];
ByteArrayOutputStream outp = new ByteArrayOutputStream();
int len = 0;
while (((len = is.read(rbs)) > 0)) {
outp.write(rbs, 0, len);
}
content = outp.toByteArray();
outp.flush();
} catch (Exception e) {
//System.out.println("Error!");
e.printStackTrace();
} finally {
if (is != null) {
is.close();
is = null;
}
}
hashcode = getHash(content,"MD5");
}
Map result = new HashMap();
result.put("hashcode", hashcode);
JSONObject jo = JSONObject.fromObject(result);
out.println(jo);
}
%>