PageRenderTime 24ms CodeModel.GetById 21ms app.highlight 0ms RepoModel.GetById 2ms app.codeStats 0ms

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