/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
- <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
- <%@ page import="java.util.*,java.io.*" %>
- <%@ page import="java.text.SimpleDateFormat" %>
- <%@ page import="org.apache.commons.fileupload.*" %>
- <%@ page import="org.apache.commons.fileupload.disk.*" %>
- <%@ page import="org.apache.commons.fileupload.servlet.*" %>
- <%@ page import="org.json.simple.*" %>
- <%
-
- /**
- * KindEditor JSP
- *
- * ?JSP???????????????????????
- * ?????????????????????????????
- *
- */
-
- //????????
- String savePath = pageContext.getServletContext().getRealPath("/") + "attached/";
-
- //??????URL
- String saveUrl = request.getContextPath() + "/attached/";
-
- //????????????
- HashMap<String, String> extMap = new HashMap<String, String>();
- extMap.put("image", "gif,jpg,jpeg,png,bmp");
- extMap.put("flash", "swf,flv");
- extMap.put("media", "swf,flv,mp3,wav,wma,wmv,mid,avi,mpg,asf,rm,rmvb");
- extMap.put("file", "doc,docx,xls,xlsx,ppt,htm,html,txt,zip,rar,gz,bz2");
-
- //??????
- long maxSize = 1000000;
-
- response.setContentType("text/html; charset=UTF-8");
-
- if(!ServletFileUpload.isMultipartContent(request)){
- out.println(getError("??????"));
- return;
- }
- //????
- File uploadDir = new File(savePath);
- if(!uploadDir.isDirectory()){
- out.println(getError("????????"));
- return;
- }
- //???????
- if(!uploadDir.canWrite()){
- out.println(getError("??????????"));
- return;
- }
-
- String dirName = request.getParameter("dir");
- if (dirName == null) {
- dirName = "image";
- }
- if(!extMap.containsKey(dirName)){
- out.println(getError("???????"));
- return;
- }
- //?????
- savePath += dirName + "/";
- saveUrl += dirName + "/";
- File saveDirFile = new File(savePath);
- if (!saveDirFile.exists()) {
- saveDirFile.mkdirs();
- }
- SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
- String ymd = sdf.format(new Date());
- savePath += ymd + "/";
- saveUrl += ymd + "/";
- File dirFile = new File(savePath);
- if (!dirFile.exists()) {
- dirFile.mkdirs();
- }
-
- FileItemFactory factory = new DiskFileItemFactory();
- ServletFileUpload upload = new ServletFileUpload(factory);
- upload.setHeaderEncoding("UTF-8");
- List items = upload.parseRequest(request);
- Iterator itr = items.iterator();
- while (itr.hasNext()) {
- FileItem item = (FileItem) itr.next();
- String fileName = item.getName();
- long fileSize = item.getSize();
- if (!item.isFormField()) {
- //??????
- if(item.getSize() > maxSize){
- out.println(getError("???????????"));
- return;
- }
- //?????
- String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
- if(!Arrays.<String>asList(extMap.get(dirName).split(",")).contains(fileExt)){
- out.println(getError("????????????????\n???" + extMap.get(dirName) + "???"));
- return;
- }
-
- SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
- String newFileName = df.format(new Date()) + "_" + new Random().nextInt(1000) + "." + fileExt;
- try{
- File uploadedFile = new File(savePath, newFileName);
- item.write(uploadedFile);
- }catch(Exception e){
- out.println(getError("???????"));
- return;
- }
-
- JSONObject obj = new JSONObject();
- obj.put("error", 0);
- obj.put("url", saveUrl + newFileName);
- out.println(obj.toJSONString());
- }
- }
- %>
- <%!
- private String getError(String message) {
- JSONObject obj = new JSONObject();
- obj.put("error", 1);
- obj.put("message", message);
- return obj.toJSONString();
- }
- %>