-
Notifications
You must be signed in to change notification settings - Fork 139
/
Copy pathufinder.java
353 lines (294 loc) · 9.59 KB
/
ufinder.java
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
package ufinder;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import upload.upload;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
* @author 徐兴繁
* yntravelsky
* 2014-11-17
* 运行环境windwos
*
*/
public class ufinder extends HttpServlet { private static final long serialVersionUID = 9171771165035078855L;
private String propath="";
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doPost(req, resp);
}
public void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
propath=request.getServletContext().getRealPath("/");
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
String ROOT = "/files";
String cmd = request.getParameter("cmd"); // $_GET["cmd"];
String target = request.getParameter("target"); // $target =
// $_GET["target"];
String array[] = { ".gif", ".png", ".jpg", ".jpeg", ".bmp" };
Map<String, Object> uploadconfig = new HashMap<String, Object>();
uploadconfig.put("savePath", "upload/");
uploadconfig.put("maxSize", 1000);
uploadconfig.put("allowFiles", array);
Map<String, Object> sysconfig = new HashMap<String, Object>();
sysconfig.put("ROOT", "/files");
sysconfig.put("write", true);
sysconfig.put("read", true);
sysconfig.put("upload", uploadconfig);
Map<String, Object> mapconfig = new HashMap<String, Object>();
Map<String, Object> map = new HashMap<String, Object>();
if (cmd.equals("init")) { //初始化
map.put("root", getFileInfo("/", ROOT));
map.put("config", mapconfig);
out.println(getJson("0", "success", map));
} else if (cmd.equals("ls")) {//文件列表
if (null != target && !target.equals(""))
;
else
target = "";
List<Map<String, Object>> list = listFile(target, ROOT);
map.put("files", list);
out.println(getJson("0", "success", map));
} else if (cmd.equals("rename")) {//重命名
String name = request.getParameter("name");
Boolean flag = rename(ROOT + target, ROOT + name);
map.put("file", getFileInfo(name, ROOT));
if (flag) {
out.println(getJson("0", "success", map));
} else {
out.println(getJson("1", "rename error", null));
}
} else if (cmd.equals("rm")) {//删除
Boolean flag = false;
String[] targets = request.getParameterValues("target[]");
for (int i = 0; i < targets.length; i++) {
flag = remove(targets[i], ROOT);
if (!flag) {
break;
}
}
if (flag) {
out.println(getJson("0", "success", null));
} else {
out.println(getJson("1", "rename error", null));
}
} else if (cmd.equals("touch")) {//新建文件
Boolean flag = file_put_contents(target, ROOT);
if (flag) {
map.put("file", getFileInfo(target, ROOT));
out.println(getJson("0", "success", map));
} else {
out.println(getJson("1", "touch error", null));
}
} else if (cmd.equals("mkdir")) {//新建目录
Boolean res = mkdir(ROOT, target);
map.put("file", getFileInfo(target, ROOT));
if (res) {
out.println(getJson("0", "success", map));
} else {
out.println(getJson("1", "error", map));
}
} else if (cmd.equals("upload")) {//上传:目前可能因ufinder还有问题
upload up = new upload();
String filename = up.uploadfile(request, response, target, ROOT);
map.put("file", getFileInfo(target + filename, ROOT));
if (filename.length() > 0) {
System.out.print(getJson("0", "success", map));
out.println(getJson("0", "success", map));
} else {
out.println(getJson("1", "fail", map));
}
} else if (cmd.equals("download")) {//下载
download(request, response, target, ROOT);
} else if (cmd.equals("info")) {
map.put("file", getFileInfo(target, ROOT));
out.println(getJson("0", "success", map));
} else {
out.println(getJson("1", "unknow command", null));
}
}
public String getJson(String state, String message, Map<String, Object> data) {
Map<String, Object> jsonmap = new HashMap<String, Object>();
jsonmap.put("state", state);
jsonmap.put("message", message);
if (data != null)
jsonmap.put("data", data);
ObjectMapper mapper = new ObjectMapper();
String json = "";
try {
json = mapper.writeValueAsString(jsonmap);
} catch (JsonProcessingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return json;
}
public List<Map<String, Object>> listFile(String dirpath, String ROOT)
throws JsonProcessingException {
String ROOTPATH =propath + ROOT;
File dir = new File(ROOTPATH + dirpath);
File[] files = dir.listFiles();
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
if (null != files) {
for (int i = 0; i < files.length; i++) {
list.add(getFileInfo("/" + files[i].getPath().substring(propath.length()+6),
ROOT));
}
}
return list;
}
public String getFileName(String path) {
int index = path.lastIndexOf("/");
if (index != -1) {
return path.substring(index);
} else {
return path;
}
}
public Map<String, Object> getFileInfo(String path, String root) {
String rootpath =propath + root + path;
String regexp = "\\\\";
path = path.replaceAll(regexp, "/");
File file = new File(rootpath);
Map<String, Object> info = new HashMap<String, Object>();
if (path.lastIndexOf(".") != -1 || path.equals("/")) {
info.put("path", path);
} else {
info.put("path", path + "/");
}
info.put("name", file.getName());
info.put("isdir", file.isDirectory());
if (path.lastIndexOf(".") != -1)
info.put("type", "file");
else {
info.put("type", "dir");
}
info.put("read", file.canRead());
info.put("write", file.canWrite());
info.put("time", file.lastModified());
// info.put("mode",);
info.put("size", file.length());
return info;
}
public Boolean rename(String oldname, String newname) {
String oldpath = propath + oldname;
String newpath = propath + newname;
File oldfile = new File(oldpath); // 指定文件名及路径
File newfile = new File(newpath);
if (newfile.exists()) {
return false;
} else {
oldfile.renameTo(newfile); // 改名
return true;
}
}
public Boolean file_put_contents(String target, String ROOT)
throws IOException {
String rootpath = propath + ROOT + target;
File file = new File(rootpath);
if (!file.exists()) {
file.createNewFile();
return true;
} else {
return false;
}
}
public Boolean mkdir(String ROOT, String path) {
String rootpath = propath + ROOT + path;
File file = new File(rootpath);
if (!file.exists()) {
file.mkdir();
return true;
}
return false;
}
/**
* 删除空目录
*
* @param dir
* 将要删除的目录路径
*/
public void doDeleteEmptyDir(String dir) {
boolean success = (new File(dir)).delete();
if (success) {
System.out.println("Successfully deleted empty directory: " + dir);
} else {
System.out.println("Failed to delete empty directory: " + dir);
}
}
/**
* 递归删除目录下的所有文件及子目录下所有文件
*
* @param dir
* 将要删除的文件目录
* @return boolean Returns "true" if all deletions were successful. If a
* deletion fails, the method stops attempting to delete and returns
* "false".
*/
public boolean deleteDir(File dir) {
if (dir.isDirectory()) {
String[] children = dir.list();
for (int i = 0; i < children.length; i++) {
boolean success = deleteDir(new File(dir, children[i]));
if (!success) {
return false;
}
}
}
// 目录此时为空,可以删除
return dir.delete();
}
public Boolean remove(String target, String ROOT) {
String ROOTPATH = propath + ROOT;
File dir = new File(ROOTPATH + target);
boolean success = deleteDir(dir);
// doDeleteEmptyDir(ROOTPATH);
return true;
}
public void download(HttpServletRequest req, HttpServletResponse resp,
String target, String ROOT) throws IOException {
String fullFilePath = req.getServletContext().getRealPath("/") + ROOT
+ target;
/*读取文件*/
File file = new File(fullFilePath);
/*如果文件存在*/
if (file.exists()) {
String filename = URLEncoder.encode(file.getName(), "UTF-8");
resp.reset();
resp.setContentType("application/x-msdownload");
resp.addHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
int fileLength = (int) file.length();
resp.setContentLength(fileLength);
/*如果文件长度大于0*/
if (fileLength != 0) {
/*创建输入流*/
InputStream inStream = new FileInputStream(file);
byte[] buf = new byte[4096];
/*创建输出流*/
ServletOutputStream servletOS = resp.getOutputStream();
int readLength;
while (((readLength = inStream.read(buf)) != -1)) {
servletOS.write(buf, 0, readLength);
}
inStream.close();
servletOS.flush();
servletOS.close();
}
}
}
}