BaseUmeditor.java
8.66 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/**
* Copyright 2015-2025 FLY的狐狸(email:jflyfox@sina.com qq:369191470).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.jflyfox.component.controller.base;
import com.jflyfox.component.base.BaseProjectController;
import com.jflyfox.util.Config;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.net.*;
import java.util.*;
/**
* Ueditor 封装
*
* @author flyfox
* 2014-1-20
*/
public class BaseUmeditor extends BaseProjectController {
/**
* 涂鸦
*
* @author flyfox 2014-1-20
*/
public void scrawlup() {
String[] fileType = { ".gif", ".png", ".jpg", ".jpeg", ".bmp" };
String param = getPara("action");
Uploader up = new Uploader(getRequest());
String path = "upload";
up.setSavePath(path);
up.setAllowFiles(fileType);
up.setMaxSize(10000); // 单位KB
if (param != null && param.equals("tmpImg")) {
try {
up.upload();
} catch (Exception e) {
e.printStackTrace();
}
renderJavascript("parent.ue_callback('" + up.getUrl() + "','" + up.getState() + "')");
} else {
up.uploadBase64("content");
renderText("{'url':'" + up.getUrl() + "',state:'" + up.getState() + "'}");
}
}
/**
* 图片上传
*
* @author flyfox 2014-1-20
*/
public void imageup() {
String[] fileType = { ".gif", ".png", ".jpg", ".jpeg", ".bmp" };
List<String> savePath = Arrays.asList(Config.getStr("savePath").split(","));
// 获取存储目录结构
if (getPara("fetch") != null) {
// 构造json数据
Iterator<String> iterator = savePath.iterator();
String dirs = "[";
while (iterator.hasNext()) {
dirs += "'" + iterator.next() + "'";
if (iterator.hasNext()) {
dirs += ",";
}
}
dirs += "]";
renderJavascript("updateSavePath( " + dirs + " );");
return;
}
Uploader up = new Uploader(getRequest());
// 获取前端提交的path路径
String dir = getPara("dir");
// 普通请求中拿不到参数, 则从上传表单中拿
if (dir == null) {
dir = up.getParameter("dir");
}
if (dir == null || "".equals(dir)) {
// 赋予默认值
dir = savePath.get(0);
// 安全验证
} else if (!savePath.contains(dir)) {
renderText("{'state':'\\u975e\\u6cd5\\u4e0a\\u4f20\\u76ee\\u5f55'}");
return;
}
try {
up.setSavePath(dir);
up.setAllowFiles(fileType);
up.setMaxSize(500 * 1024); // 单位KB
up.upload();
} catch (Exception e) {
e.printStackTrace();
}
renderText("{'original':'" + up.getOriginalName() + "','url':'" + up.getUrl() + "','title':'" + up.getTitle()
+ "','state':'" + up.getState() + "'}");
}
/**
* 图片管理
*
* @author flyfox 2014-1-20
*/
public void imagemanager() {
// 仅做示例用,请自行修改
String path = "upload";
String imgStr = "";
String realpath = getRealPath(getRequest(), path) + "/" + path;
List<File> files = getFiles(realpath, new ArrayList<File>());
for (File file : files) {
imgStr += file.getPath().replace(getRealPath(getRequest(), path), "") + "ue_separate_ue";
}
if (imgStr != "") {
imgStr = imgStr.substring(0, imgStr.lastIndexOf("ue_separate_ue")).replace(File.separator, "/").trim();
}
renderText(imgStr);
}
protected List<File> getFiles(String realpath, List<File> files) {
File realFile = new File(realpath);
if (realFile.isDirectory()) {
File[] subfiles = realFile.listFiles();
for (File file : subfiles) {
if (file.isDirectory()) {
getFiles(file.getAbsolutePath(), files);
} else {
if (!getFileTypeByimageManager(file.getName()).equals("")) {
files.add(file);
}
}
}
}
return files;
}
protected String getRealPath(HttpServletRequest request, String path) {
ServletContext application = request.getSession().getServletContext();
String str = application.getRealPath(request.getServletPath());
return new File(str).getParent();
}
protected String getFileTypeByimageManager(String fileName) {
String[] fileType = { ".gif", ".png", ".jpg", ".jpeg", ".bmp" };
Iterator<String> type = Arrays.asList(fileType).iterator();
while (type.hasNext()) {
String t = type.next();
if (fileName.toLowerCase().endsWith(t)) {
return t;
}
}
return "";
}
/**
* 附件上传
*
* @author flyfox 2014-1-20
*/
public void fileup() {
String[] fileType = { ".rar", ".doc", ".docx", ".zip", ".pdf", ".txt", ".swf", ".wmv", ".avi", ".rm", ".rmvb",
".mpeg", ".mpg", ".ogg", ".mov", ".wmv", ".mp4" }; // 允许的文件类型
Uploader up = new Uploader(getRequest());
up.setSavePath("upload"); // 保存路径
up.setAllowFiles(fileType);
up.setMaxSize(500 * 1024); // 允许的文件最大尺寸,单位KB
try {
up.upload();
} catch (Exception e) {
e.printStackTrace();
}
renderText("{'url':'" + up.getUrl() + "','fileType':'" + up.getType() + "','state':'" + up.getState()
+ "','original':'" + up.getOriginalName() + "'}");
}
/**
* 视频上传
*
* @author flyfox 2014-1-20
*/
public void getMovie() {
StringBuffer readOneLineBuff = new StringBuffer();
String content = "";
String searchkey = getPara("searchKey");
String videotype = getPara("videoType");
try {
searchkey = URLEncoder.encode(searchkey, "utf-8");
URL url = new URL("http://api.tudou.com/v3/gw?method=item.search&appKey=myKey&format=json&kw=" + searchkey
+ "&pageNo=1&pageSize=20&channelId=" + videotype + "&inDays=7&media=v&sort=s");
URLConnection conn = url.openConnection();
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
String line = "";
while ((line = reader.readLine()) != null) {
readOneLineBuff.append(line);
}
content = readOneLineBuff.toString();
reader.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e2) {
e2.printStackTrace();
}
renderText(content);
}
/**
* 网络图片
*
* @author flyfox 2014-1-20
*/
public void getremoteimage() {
String url = getPara("upfile");
String state = "远程图片抓取成功!";
String filePath = "upload";
String[] arr = url.split("ue_separate_ue");
String[] outSrc = new String[arr.length];
for (int i = 0; i < arr.length; i++) {
// 保存文件路径
@SuppressWarnings("deprecation")
String str = getRequest().getRealPath(getRequest().getServletPath());
File f = new File(str);
String savePath = f.getParent() + "/" + filePath;
// 格式验证
String type = getFileTypeByRemoteImage(arr[i]);
if (type.equals("")) {
state = "图片类型不正确!";
continue;
}
String saveName = Long.toString(new Date().getTime()) + type;
// 大小验证
HttpURLConnection.setFollowRedirects(false);
HttpURLConnection conn = null;
try {
conn = (HttpURLConnection) new URL(arr[i]).openConnection();
} catch (IOException e) {
e.printStackTrace();
}
if (conn.getContentType().indexOf("image") == -1) {
state = "请求地址头不正确";
continue;
}
try {
if (conn.getResponseCode() != 200) {
state = "请求地址不存在!";
continue;
}
} catch (IOException e1) {
e1.printStackTrace();
}
File dir = new File(savePath);
if (!dir.exists()) {
dir.mkdirs();
}
File savetoFile = new File(savePath + "/" + saveName);
outSrc[i] = filePath + "/" + saveName;
try {
InputStream is = conn.getInputStream();
OutputStream os = new FileOutputStream(savetoFile);
int b;
while ((b = is.read()) != -1) {
os.write(b);
}
os.close();
is.close();
// 这里处理 inputStream
} catch (Exception e) {
e.printStackTrace();
System.err.println("页面无法访问");
}
}
String outstr = "";
for (int i = 0; i < outSrc.length; i++) {
outstr += outSrc[i] + "ue_separate_ue";
}
outstr = outstr.substring(0, outstr.lastIndexOf("ue_separate_ue"));
renderText("{'url':'" + outstr + "','tip':'" + state + "','srcUrl':'" + url + "'}");
}
protected String getFileTypeByRemoteImage(String fileName) {
String[] fileType = { ".gif", ".png", ".jpg", ".jpeg", ".bmp" };
Iterator<String> type = Arrays.asList(fileType).iterator();
while (type.hasNext()) {
String t = type.next();
if (fileName.endsWith(t)) {
return t;
}
}
return "";
}
}