/source/Plug-in/kind/jsp/upload_json.jsp

http://prosporous.googlecode.com/ · JavaServer Pages · 122 lines · 95 code · 11 blank · 16 comment · 12 complexity · ab19ad6e534bfc11712e1efea4fa9a99 MD5 · raw file

  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.apache.commons.fileupload.*" %>
  5. <%@ page import="org.apache.commons.fileupload.disk.*" %>
  6. <%@ page import="org.apache.commons.fileupload.servlet.*" %>
  7. <%@ page import="org.json.simple.*" %>
  8. <%
  9. /**
  10. * KindEditor JSP
  11. *
  12. * ?JSP???????????????????????
  13. * ?????????????????????????????
  14. *
  15. */
  16. //????????
  17. String savePath = pageContext.getServletContext().getRealPath("/") + "attached/";
  18. //??????URL
  19. String saveUrl = request.getContextPath() + "/attached/";
  20. //????????????
  21. HashMap<String, String> extMap = new HashMap<String, String>();
  22. extMap.put("image", "gif,jpg,jpeg,png,bmp");
  23. extMap.put("flash", "swf,flv");
  24. extMap.put("media", "swf,flv,mp3,wav,wma,wmv,mid,avi,mpg,asf,rm,rmvb");
  25. extMap.put("file", "doc,docx,xls,xlsx,ppt,htm,html,txt,zip,rar,gz,bz2");
  26. //??????
  27. long maxSize = 1000000;
  28. response.setContentType("text/html; charset=UTF-8");
  29. if(!ServletFileUpload.isMultipartContent(request)){
  30. out.println(getError("??????"));
  31. return;
  32. }
  33. //????
  34. File uploadDir = new File(savePath);
  35. if(!uploadDir.isDirectory()){
  36. out.println(getError("????????"));
  37. return;
  38. }
  39. //???????
  40. if(!uploadDir.canWrite()){
  41. out.println(getError("??????????"));
  42. return;
  43. }
  44. String dirName = request.getParameter("dir");
  45. if (dirName == null) {
  46. dirName = "image";
  47. }
  48. if(!extMap.containsKey(dirName)){
  49. out.println(getError("???????"));
  50. return;
  51. }
  52. //?????
  53. savePath += dirName + "/";
  54. saveUrl += dirName + "/";
  55. File saveDirFile = new File(savePath);
  56. if (!saveDirFile.exists()) {
  57. saveDirFile.mkdirs();
  58. }
  59. SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
  60. String ymd = sdf.format(new Date());
  61. savePath += ymd + "/";
  62. saveUrl += ymd + "/";
  63. File dirFile = new File(savePath);
  64. if (!dirFile.exists()) {
  65. dirFile.mkdirs();
  66. }
  67. FileItemFactory factory = new DiskFileItemFactory();
  68. ServletFileUpload upload = new ServletFileUpload(factory);
  69. upload.setHeaderEncoding("UTF-8");
  70. List items = upload.parseRequest(request);
  71. Iterator itr = items.iterator();
  72. while (itr.hasNext()) {
  73. FileItem item = (FileItem) itr.next();
  74. String fileName = item.getName();
  75. long fileSize = item.getSize();
  76. if (!item.isFormField()) {
  77. //??????
  78. if(item.getSize() > maxSize){
  79. out.println(getError("???????????"));
  80. return;
  81. }
  82. //?????
  83. String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
  84. if(!Arrays.<String>asList(extMap.get(dirName).split(",")).contains(fileExt)){
  85. out.println(getError("????????????????\n???" + extMap.get(dirName) + "???"));
  86. return;
  87. }
  88. SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
  89. String newFileName = df.format(new Date()) + "_" + new Random().nextInt(1000) + "." + fileExt;
  90. try{
  91. File uploadedFile = new File(savePath, newFileName);
  92. item.write(uploadedFile);
  93. }catch(Exception e){
  94. out.println(getError("???????"));
  95. return;
  96. }
  97. JSONObject obj = new JSONObject();
  98. obj.put("error", 0);
  99. obj.put("url", saveUrl + newFileName);
  100. out.println(obj.toJSONString());
  101. }
  102. }
  103. %>
  104. <%!
  105. private String getError(String message) {
  106. JSONObject obj = new JSONObject();
  107. obj.put("error", 1);
  108. obj.put("message", message);
  109. return obj.toJSONString();
  110. }
  111. %>