imgScaling.jsp
5.83 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<%@page import="java.util.Iterator"%>
<%@page import="net.sf.json.JSONObject"%>
<%@page import="net.sf.json.JSONArray"%>
<%@page import="com.weaver.formmodel.util.NumberHelper"%>
<%@page import="java.util.zip.ZipEntry"%>
<%@page import="com.mortennobel.imagescaling.ResampleOp"%>
<%@page import="javax.imageio.ImageIO"%>
<%@page import="java.awt.image.BufferedImage"%>
<%@page import="java.io.BufferedInputStream"%>
<%@page import="java.io.File"%>
<%@page import="java.io.FileInputStream"%>
<%@page import="java.io.IOException"%>
<%@page import="java.io.InputStream"%>
<%@page import="java.net.URLEncoder"%>
<%@page import="java.util.zip.ZipInputStream"%>
<%@page import="weaver.conn.RecordSet"%>
<%@page import="weaver.file.AESCoder"%>
<%@page import="weaver.general.Util"%>
<%@ page language="java"%>
<%@ include file="/mobilemode/mobile/base.jsp"%>
<%
response.resetBuffer();
String fileid = Util.null2String(request.getParameter("fileid")).trim();
int sIndex;
if((sIndex = fileid.indexOf("/weaver/weaver.file.FileDownload?fileid=")) != -1){
int eIndex = fileid.indexOf("&", sIndex);
if(eIndex == -1){
eIndex = fileid.length();
}
fileid = fileid.substring(sIndex + "/weaver/weaver.file.FileDownload?fileid=".length(), eIndex);
}
boolean isID = fileid.matches("^[-+]?(([0-9]+)([.]([0-9]+))?|([.]([0-9]+))?)$");
boolean isUrl = !isID && !fileid.equals("");
InputStream inStream = null;
String filename = "";
long filesize = -1;
String imagefiletype = "";
if(isID){
String sql = "select t1.imagefilename,t1.filerealpath,t1.iszip,t1.isencrypt,t1.imagefiletype , t1.imagefileid, t1.imagefile,t1.isaesencrypt,t1.aescode,t2.imagefilename as realname,t1.TokenKey,t1.StorageStatus,t1.comefrom,t1.fileSize as filesize from ImageFile t1 left join DocImageFile t2 on t1.imagefileid = t2.imagefileid where t1.imagefileid = "+fileid;
RecordSet rs = new RecordSet();
rs.execute(sql);
if(rs.next()){
filename = Util.null2String(rs.getString("realname"));
if(filename.equals("")){
filename = Util.null2String(rs.getString("imagefilename"));
}
String filerealpath = Util.null2String(rs.getString("filerealpath"));
String iszip = Util.null2String(rs.getString("iszip"));
String isaesencrypt = Util.null2o(rs.getString("isaesencrypt"));
String aescode = Util.null2String(rs.getString("aescode"));
imagefiletype = Util.null2String(rs.getString("imagefiletype"));
try{
filesize = Long.parseLong(rs.getString("filesize"));
}catch(Exception ex){
filesize = -1;
ex.printStackTrace();
}
ZipInputStream zin = null;
File thefile = new File(filerealpath);
if (iszip.equals("1")) {
zin = new ZipInputStream(new FileInputStream(thefile));
if (zin.getNextEntry() != null) inStream = new BufferedInputStream(zin);
} else{
inStream = new BufferedInputStream(new FileInputStream(thefile));
}
if(isaesencrypt.equals("1")){
inStream = AESCoder.decrypt(inStream, aescode);
}
}
}else if(isUrl){
String realPath = request.getSession().getServletContext().getRealPath(fileid);
File file = new File(realPath);
if(!file.exists() || !file.isFile()){
return;
}
inStream = new BufferedInputStream(new FileInputStream(file));
filename = file.getName();
filesize = file.length();
}
String extName = "";
if(filename.indexOf(".") > -1){
int bx = filename.lastIndexOf(".");
if(bx>=0){
extName = filename.substring(bx+1, filename.length());
}
}
String contenttype = "";
if(filename.toLowerCase().endsWith(".gif")) {
contenttype = "image/gif";
response.addHeader("Cache-Control", "private, max-age=8640000");
}else if(filename.toLowerCase().endsWith(".png")) {
contenttype = "image/png";
response.addHeader("Cache-Control", "private, max-age=8640000");
}else if(filename.toLowerCase().endsWith(".jpg")) {
contenttype = "image/jpg";
response.addHeader("Cache-Control", "private, max-age=8640000");
}else if(filename.toLowerCase().endsWith(".bmp")) {
contenttype = "image/bmp";
response.addHeader("Cache-Control", "private, max-age=8640000");
}else {
contenttype = imagefiletype;
}
response.setContentType(contenttype);
try {
response.setHeader("content-disposition", "inline; filename=\"" + URLEncoder.encode(filename,"UTF-8")+"\"");
} catch (Exception ecode) {}
ServletOutputStream outStream = null;
try {
double ratio;
String ratioStr = Util.null2String(request.getParameter("ratio"));
if(!ratioStr.trim().equals("")){
try{
ratio = Double.parseDouble(ratioStr);
}catch(Exception ex){
ex.printStackTrace();
ratio = 1;
}
}else{
ratio = 1;
String ratioRule = Util.null2String(request.getParameter("ratioRule"));
if(ratioRule.trim().equals("")){
if(extName.equalsIgnoreCase("png")){
ratioRule = "{20:0.7, 40:0.6, 60:0.5, 80:0.4, 100:0.3}";
}else{
ratioRule = "{20:0.9, 50:0.8, 100:0.7, 150:0.6, 200:0.5, 500:0.4, 1024:0.3}";
}
}
JSONObject ratioRuleObj = JSONObject.fromObject(ratioRule);
long filesizeKb = filesize / 1024;
Iterator iterator = ratioRuleObj.keys();
while(iterator.hasNext()){
Object rKey = iterator.next();
int rk = NumberHelper.getIntegerValue(rKey);
if(filesizeKb >= rk){
double rv;
try{
rv = Double.parseDouble(ratioRuleObj.get(rKey).toString());
}catch(Exception ex){
ex.printStackTrace();
rv = 1;
}
if(rv < ratio){
ratio = rv;
}
}
}
}
BufferedImage inputBufImage = ImageIO.read(inStream);
int width = (int)(inputBufImage.getWidth() * ratio);
int height = (int)(inputBufImage.getHeight() * ratio);
ResampleOp resampleOp = new ResampleOp(width, height);
BufferedImage rescaledTomato = resampleOp.filter(inputBufImage,null);
if(extName.equals("")){
extName = "jpg";
}
outStream = response.getOutputStream();
ImageIO.write(rescaledTomato, extName, outStream);
}
catch(Exception e) {
e.printStackTrace();
}
finally {
if(inStream!=null) inStream.close();
if(outStream!=null) outStream.flush();
if(outStream!=null) outStream.close();
}
%>