picsetAction.jsp
2.69 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
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ page import="java.io.FileFilter"%>
<%@ page import="net.sf.json.JSONObject"%>
<%@ page import="net.sf.json.JSONArray"%>
<%@ page import="weaver.general.GCONST"%>
<%@ page import="java.io.File"%>
<%@ page import="java.net.URLDecoder"%>
<%@ include file="/mobilemode/init.jsp"%>
<%
response.reset();
out.clear();
String action = Util.null2String(request.getParameter("action"));
if(action.equalsIgnoreCase("getServerPicDatas")){
JSONObject resultObj = new JSONObject();
try{
String path = Util.null2String(request.getParameter("path"));
path = URLDecoder.decode(path, "UTF-8");
if(path.equals("")){
path = "/mobilemode/piclibrary";
}
if(!path.startsWith("/mobilemode/piclibrary") || path.indexOf("../") != -1){
throw new IllegalArgumentException("路径拒绝访问");
}
resultObj.put("path", path);
JSONArray jsonArray = new JSONArray();
final List<String> acceptList = Arrays.asList(".jpg", ".jpeg", ".png", ".bmp", ".gif", ".ico");
final List<String> ignoreFolders = Arrays.asList("favor", "icon", "icon2", "icon3", "icon4", "icon5", "nav", "tbar");
String realPath = session.getServletContext().getRealPath(path);
File file = new File(realPath);
File[] childFileArr = file.listFiles(new FileFilter() {
public boolean accept(File file) {
boolean result = false;
if(!file.isHidden()){
String name = file.getName();
if(file.isDirectory()){
return !ignoreFolders.contains(name);
}else{
if(name.indexOf(".") != -1){
String s = name.substring(name.lastIndexOf(".")).toLowerCase();
if(acceptList.contains(s)){
result = true;
}
}
}
}
return result;
}
});
for(int i = 0; childFileArr != null && i < childFileArr.length; i++){
File childFile = childFileArr[i];
String fileServerPath = path + "/" + childFile.getName();
boolean isFolder = childFile.isDirectory();
String filename = childFile.getName();
JSONObject jsonObject = new JSONObject();
jsonObject.put("fileServerPath", fileServerPath);
jsonObject.put("isFolder", isFolder);
jsonObject.put("filename", filename);
jsonArray.add(jsonObject);
}
Collections.sort(jsonArray, new Comparator<JSONObject>() {
public int compare(JSONObject o1, JSONObject o2) {
boolean isFolder = (Boolean)o1.get("isFolder");
boolean isFolder2 = (Boolean)o2.get("isFolder");
return (isFolder && isFolder2) ? 0 : (isFolder ? -1 : 1);
}
});
resultObj.put("files", jsonArray);
resultObj.put("status", "1");
}catch(Exception ex){
ex.printStackTrace();
resultObj.put("status", "0");
}finally{
out.print(resultObj);
}
}
out.flush();
out.close();
%>