FileManagerController.java
4.25 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
package com.jflyfox.modules.filemanager;
import java.io.UnsupportedEncodingException;
import javax.servlet.http.HttpServletRequest;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.log.Log;
import com.jflyfox.component.base.BaseProjectController;
import com.jflyfox.jfinal.component.annotation.ControllerBind;
/**
* 文件管理
*
* 2016年2月26日 下午2:33:03 flyfox 369191470@qq.com
*/
@ControllerBind(controllerKey = "/admin/filemanager")
public class FileManagerController extends BaseProjectController {
private final static Log log = Log.getLog(FileManagerController.class);
private static final String path = "/pages/admin/filemanager/";
public void list() {
render(path + "index.html");
}
/**
* 跳转到操作页面
*
* 2015年3月16日 下午5:33:55 flyfox 369191470@qq.com
*/
public void index() {
HttpServletRequest request = getRequest();
try {
request.setCharacterEncoding("UTF-8");
getResponse().setCharacterEncoding("UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
FileManager fm = new FileManager(getRequest());
JSONObject responseData = null;
String mode = "";
String path = "";
boolean needPath = false;
boolean putTextarea = false;
if (!auth()) {
fm.error(fm.lang("AUTHORIZATION_REQUIRED"));
} else {
String contextPath = request.getContextPath();
// 设置contextPath
fm.setGetVar("contextPath", contextPath);
mode = request.getParameter("mode");
path = request.getParameter("path");
if (path != null) {
try {
if (request.getMethod().equals("GET"))
path = new String(path.getBytes("ISO8859-1"), "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
needPath = fm.setGetVar("path", path);
}
if (request.getMethod().equals("GET")) {
if (mode != null && mode != "") {
if (mode.equals("getinfo")) {
if (needPath) {
responseData = fm.getInfo();
}
} else if (mode.equals("getfolder")) {
if (needPath) {
responseData = fm.getFolder();
}
} else if (mode.equals("rename")) {
String oldFile = request.getParameter("old");
String newFile = request.getParameter("new");
try {
oldFile = new String(oldFile.getBytes("ISO8859-1"), "UTF-8");
newFile = new String(newFile.getBytes("ISO8859-1"), "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
if (fm.setGetVar("old", oldFile) && fm.setGetVar("new", newFile)) {
responseData = fm.rename();
}
} else if (mode.equals("delete")) {
if (needPath) {
responseData = fm.delete();
}
} else if (mode.equals("addfolder")) {
String name = request.getParameter("name");
try {
name = new String(name.getBytes("ISO8859-1"), "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
if (needPath && fm.setGetVar("name", name)) {
responseData = fm.addFolder();
}
} else if (mode.equals("download")) {
if (needPath) {
fm.download(getResponse());
}
} else if (mode.equals("preview")) {
if (needPath) {
fm.preview(getResponse());
}
} else if (mode.equals("editfile")) {
if (needPath) {
responseData = fm.editFile();
}
} else {
fm.error(fm.lang("MODE_ERROR"));
}
}
} else if (request.getMethod().equals("POST")) {
if (mode == null) {
mode = "upload";
responseData = fm.add();
putTextarea = true;
} else if (mode.equals("savefile")) {
if (needPath && fm.setGetContent("content", request.getParameter("content"))) {
responseData = fm.saveFile();
}
}
}
}
if (responseData == null) {
responseData = fm.getError();
}
if (responseData != null) {
String responseStr = responseData.toString();
if (putTextarea)
responseStr = "<textarea>" + responseStr + "</textarea>";
log.info("mode:" + mode + ",response:" + responseStr);
renderText(responseStr);
} else {
renderNull();
}
}
/**
* 管理员才可以
*
* 2016年3月6日 上午9:39:10
* flyfox 369191470@qq.com
* @return
*/
public boolean auth() {
return getSessionUser() != null && getSessionUser().getInt("usertype") == 1;
}
}