file_manager_json.jsp.txt 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
  2. <%@ page import="java.util.*,java.io.*" %>
  3. <%@ page import="java.text.SimpleDateFormat" %>
  4. <%@ page import="org.json.simple.*" %>
  5. <%
  6. /**
  7. * KindEditor JSP
  8. *
  9. * 本JSP程序是演示程序,建议不要直接在实际项目中使用。
  10. * 如果您确定直接使用本程序,使用之前请仔细确认相关安全设置。
  11. *
  12. */
  13. //根目录路径,可以指定绝对路径,比如 /var/www/attached/
  14. String rootPath = pageContext.getServletContext().getRealPath("/") + "attached/";
  15. //根目录URL,可以指定绝对路径,比如 http://www.yoursite.com/attached/
  16. String rootUrl = request.getContextPath() + "/attached/";
  17. //图片扩展名
  18. String[] fileTypes = new String[]{"gif", "jpg", "jpeg", "png", "bmp"};
  19. String dirName = request.getParameter("dir");
  20. if (dirName != null) {
  21. if(!Arrays.<String>asList(new String[]{"image", "flash", "media", "file"}).contains(dirName)){
  22. out.println("Invalid Directory name.");
  23. return;
  24. }
  25. rootPath += dirName + "/";
  26. rootUrl += dirName + "/";
  27. File saveDirFile = new File(rootPath);
  28. if (!saveDirFile.exists()) {
  29. saveDirFile.mkdirs();
  30. }
  31. }
  32. //根据path参数,设置各路径和URL
  33. String path = request.getParameter("path") != null ? request.getParameter("path") : "";
  34. String currentPath = rootPath + path;
  35. String currentUrl = rootUrl + path;
  36. String currentDirPath = path;
  37. String moveupDirPath = "";
  38. if (!"".equals(path)) {
  39. String str = currentDirPath.substring(0, currentDirPath.length() - 1);
  40. moveupDirPath = str.lastIndexOf("/") >= 0 ? str.substring(0, str.lastIndexOf("/") + 1) : "";
  41. }
  42. //排序形式,name or size or type
  43. String order = request.getParameter("order") != null ? request.getParameter("order").toLowerCase() : "name";
  44. //不允许使用..移动到上一级目录
  45. if (path.indexOf("..") >= 0) {
  46. out.println("Access is not allowed.");
  47. return;
  48. }
  49. //最后一个字符不是/
  50. if (!"".equals(path) && !path.endsWith("/")) {
  51. out.println("Parameter is not valid.");
  52. return;
  53. }
  54. //目录不存在或不是目录
  55. File currentPathFile = new File(currentPath);
  56. if(!currentPathFile.isDirectory()){
  57. out.println("Directory does not exist.");
  58. return;
  59. }
  60. //遍历目录取的文件信息
  61. List<Hashtable> fileList = new ArrayList<Hashtable>();
  62. if(currentPathFile.listFiles() != null) {
  63. for (File file : currentPathFile.listFiles()) {
  64. Hashtable<String, Object> hash = new Hashtable<String, Object>();
  65. String fileName = file.getName();
  66. if(file.isDirectory()) {
  67. hash.put("is_dir", true);
  68. hash.put("has_file", (file.listFiles() != null));
  69. hash.put("filesize", 0L);
  70. hash.put("is_photo", false);
  71. hash.put("filetype", "");
  72. } else if(file.isFile()){
  73. String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
  74. hash.put("is_dir", false);
  75. hash.put("has_file", false);
  76. hash.put("filesize", file.length());
  77. hash.put("is_photo", Arrays.<String>asList(fileTypes).contains(fileExt));
  78. hash.put("filetype", fileExt);
  79. }
  80. hash.put("filename", fileName);
  81. hash.put("datetime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(file.lastModified()));
  82. fileList.add(hash);
  83. }
  84. }
  85. if ("size".equals(order)) {
  86. Collections.sort(fileList, new SizeComparator());
  87. } else if ("type".equals(order)) {
  88. Collections.sort(fileList, new TypeComparator());
  89. } else {
  90. Collections.sort(fileList, new NameComparator());
  91. }
  92. JSONObject result = new JSONObject();
  93. result.put("moveup_dir_path", moveupDirPath);
  94. result.put("current_dir_path", currentDirPath);
  95. result.put("current_url", currentUrl);
  96. result.put("total_count", fileList.size());
  97. result.put("file_list", fileList);
  98. response.setContentType("application/json; charset=UTF-8");
  99. out.println(result.toJSONString());
  100. %>
  101. <%!
  102. public class NameComparator implements Comparator {
  103. public int compare(Object a, Object b) {
  104. Hashtable hashA = (Hashtable)a;
  105. Hashtable hashB = (Hashtable)b;
  106. if (((Boolean)hashA.get("is_dir")) && !((Boolean)hashB.get("is_dir"))) {
  107. return -1;
  108. } else if (!((Boolean)hashA.get("is_dir")) && ((Boolean)hashB.get("is_dir"))) {
  109. return 1;
  110. } else {
  111. return ((String)hashA.get("filename")).compareTo((String)hashB.get("filename"));
  112. }
  113. }
  114. }
  115. public class SizeComparator implements Comparator {
  116. public int compare(Object a, Object b) {
  117. Hashtable hashA = (Hashtable)a;
  118. Hashtable hashB = (Hashtable)b;
  119. if (((Boolean)hashA.get("is_dir")) && !((Boolean)hashB.get("is_dir"))) {
  120. return -1;
  121. } else if (!((Boolean)hashA.get("is_dir")) && ((Boolean)hashB.get("is_dir"))) {
  122. return 1;
  123. } else {
  124. if (((Long)hashA.get("filesize")) > ((Long)hashB.get("filesize"))) {
  125. return 1;
  126. } else if (((Long)hashA.get("filesize")) < ((Long)hashB.get("filesize"))) {
  127. return -1;
  128. } else {
  129. return 0;
  130. }
  131. }
  132. }
  133. }
  134. public class TypeComparator implements Comparator {
  135. public int compare(Object a, Object b) {
  136. Hashtable hashA = (Hashtable)a;
  137. Hashtable hashB = (Hashtable)b;
  138. if (((Boolean)hashA.get("is_dir")) && !((Boolean)hashB.get("is_dir"))) {
  139. return -1;
  140. } else if (!((Boolean)hashA.get("is_dir")) && ((Boolean)hashB.get("is_dir"))) {
  141. return 1;
  142. } else {
  143. return ((String)hashA.get("filetype")).compareTo((String)hashB.get("filetype"));
  144. }
  145. }
  146. }
  147. %>