效果图
kindeditor包下类
UploadAccessory.java
/* */ package com.scrh.web.com.elkan.kindeditor;
/* */
/* */ import java.io.File;
/* */ import java.io.IOException;
/* */ import java.io.PrintStream;
/* */ import java.io.PrintWriter;
/* */ import java.text.SimpleDateFormat;
/* */ import java.util.Arrays;
/* */ import java.util.Date;
/* */ import java.util.Iterator;
/* */ import java.util.List;
/* */ import java.util.Random;
/* */ import javax.servlet.ServletContext;
/* */ import javax.servlet.ServletException;
/* */ import javax.servlet.http.HttpServlet;
/* */ import javax.servlet.http.HttpServletRequest;
/* */ import javax.servlet.http.HttpServletResponse;
/* */ import javax.servlet.http.HttpSession;
/* */ import org.apache.commons.fileupload.FileItem;
/* */ import org.apache.commons.fileupload.FileItemFactory;
/* */ import org.apache.commons.fileupload.disk.DiskFileItemFactory;
/* */ import org.apache.commons.fileupload.servlet.ServletFileUpload;
/* */
/* */ public class UploadAccessory extends HttpServlet
/* */ {
/* */ private static final long serialVersionUID = 1L;
/* 28 */ protected long MAX_SIZE = 1000000L;
/* */
/* 30 */ protected String[] FILETYPES = { "doc", "xls", "ppt", "pdf", "txt", "rar", "zip" };
/* */
/* 32 */ protected String UPLOAD_PATH = "";
/* */
/* 34 */ protected String id = "";
/* */
/* 36 */ protected String attachTitle = "";
/* */
/* 38 */ protected boolean isFlag = false;
/* */
/* 40 */ protected String tempTitle = "";
/* */
/* */ public void doGet(HttpServletRequest request, HttpServletResponse response)
/* */ throws ServletException, IOException
/* */ {
/* 45 */ doPost(request, response);
/* */ }
/* */
/* */ public void doPost(HttpServletRequest request, HttpServletResponse response)
/* */ throws ServletException, IOException
/* */ {
/* 52 */ response.setContentType("text/html; charset=UTF-8");
/* 53 */ PrintWriter out = response.getWriter();
/* 54 */ String savePath = getInitParameter("UPLOAD_PATH");
/* 55 */ if ((savePath == null) || (savePath.isEmpty())) {
/* 56 */ out.println(alertMsg("你还没设置上传文件保存的目录路径!"));
/* 57 */ return;
/* */ }
/* */
/* 60 */ if (getInitParameter("MAX_SIZE") != null) {
/* 61 */ this.MAX_SIZE = Integer.parseInt(getInitParameter("MAX_SIZE"));
/* */ }
/* */
/* 64 */ if (getInitParameter("FILETYPES") != null) {
/* 65 */ this.FILETYPES = toArray(getInitParameter("FILETYPES"));
/* */ }
/* */
/* 68 */ String uploadPath = request.getSession().getServletContext().getRealPath("/") + savePath;
/* */
/* 71 */ String saveUrl = request.getContextPath() + "/" + savePath;
/* */
/* 73 */ if (!ServletFileUpload.isMultipartContent(request)) {
/* 74 */ out.println(alertMsg("请选择要上传的文件。"));
/* 75 */ return;
/* */ }
/* */
/* 78 */ File uploadDir = new File(uploadPath);
/* 79 */ if (!uploadDir.isDirectory()) {
/* 80 */ out.println(alertMsg("上传目录不存在。"));
/* 81 */ return;
/* */ }
/* */
/* 84 */ if (!uploadDir.canWrite()) {
/* 85 */ out.println(alertMsg("当前角色对上传目录没有写权限。"));
/* 86 */ return;
/* */ }
/* */
/* 89 */ FileItemFactory factory = new DiskFileItemFactory();
/* 90 */ ServletFileUpload upload = new ServletFileUpload(factory);
/* 91 */ upload.setHeaderEncoding("UTF-8");
/* 92 */ String temp = null;
/* 93 */ String ext = null;
/* */ try {
/* 95 */ List items = upload.parseRequest(request);
/* 96 */ Iterator itr = items.iterator();
/* 97 */ while (itr.hasNext()) {
/* 98 */ FileItem item = (FileItem)itr.next();
/* 99 */ String fileName = item.getName();
/* 100 */ temp = item.getName();
/* 101 */ if ((temp != null) && (!this.isFlag)) {
/* 102 */ temp = temp.substring(temp.lastIndexOf("\\") + 1);
/* 103 */ this.tempTitle = temp;
/* 104 */ this.isFlag = true;
/* */ }
/* */
/* 107 */ if (item.getFieldName().equals("id")) {
/* 108 */ this.id = item.getString();
/* */ }
/* */
/* 111 */ if (item.getFieldName().equals("attachTitle")) {
/* 112 */ this.attachTitle = item.getString();
/* 113 */ if (this.attachTitle != null) {
/* 114 */ this.attachTitle = new String(this.attachTitle.getBytes("ISO8859-1"), "UTF-8");
/* */ }
/* */ }
/* 117 */ if (item.isFormField())
/* */ continue;
/* 119 */ if (item.getSize() > this.MAX_SIZE) {
/* 120 */ out.println(alertMsg("上传文件大小超过限制。"));
/* 121 */ return;
/* */ }
/* */
/* 124 */ String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
/* 125 */ if (!Arrays.asList(this.FILETYPES).contains(fileExt)) {
/* 126 */ out.println(alertMsg("上传文件扩展名是不允许的扩展名。"));
/* 127 */ return;
/* */ }
/* */
/* 130 */ SimpleDateFormat folderNameFormat = new SimpleDateFormat("yyyyMMdd");
/* 131 */ String realPath = uploadPath + folderNameFormat.format(new Date());
/* 132 */ File folder = new File(realPath);
/* 133 */ boolean flag = folder.exists();
/* */
/* 135 */ if (!flag) {
/* 136 */ flag = folder.mkdir();
/* */ }
/* */
/* 139 */ if (flag) {
/* 140 */ SimpleDateFormat fileNameFormat = new SimpleDateFormat("yyyyMMddHHmmss");
/* 141 */ String newFileName = fileNameFormat.format(new Date()) + "_" + new Random().nextInt(1000) + "." + fileExt;
/* 142 */ File uploadedFile = new File(realPath, newFileName);
/* 143 */ item.write(uploadedFile);
/* 144 */ saveUrl = saveUrl + folderNameFormat.format(new Date()) + "/" + newFileName;
/* 145 */ ext = fileExt;
/* */ } else {
/* 147 */ System.out.println(" 文件夹创建失败,请确认磁盘没有写保护并且空件足够");
/* */ }
/* */
/* */ }
/* */
/* 153 */ if ((this.attachTitle == null) || (this.attachTitle.isEmpty())) {
/* 154 */ this.attachTitle = this.tempTitle;
/* */ }
/* */
/* 157 */ out.println(insertAttach(this.id, saveUrl, this.attachTitle, ext));
/* */ }
/* */ catch (Exception e) {
/* 160 */ e.printStackTrace();
/* */ } finally {
/* 162 */ out.flush();
/* 163 */ out.close();
/* 164 */ this.isFlag = false;
/* */ }
/* */ }
/* */
/* */ public String alertMsg(String message)
/* */ {
/* 170 */ StringBuilder sb = new StringBuilder("<html>");
/* 171 */ sb.append("<head>").append("<title>error</title>");
/* 172 */ sb.append("<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\">");
/* 173 */ sb.append("</head>");
/* 174 */ sb.append("<body>");
/* 175 */ sb.append("<script type=\"text/javascript\">");
/* 176 */ sb.append("alert(\"").append(message).append("\");history.back();</script>");
/* 177 */ sb.append("</body>").append("</html>");
/* */
/* 179 */ return sb.toString();
/* */ }
/* */
/* */ public String insertAttach(String id, String url, String title, String ext) {
/* 183 */ StringBuilder sb = new StringBuilder("<html>");
/* 184 */ sb.append("<head>").append("<title>Insert Accessory</title>");
/* 185 */ sb.append("<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\">");
/* 186 */ sb.append("</head>");
/* 187 */ sb.append("<body>");
/* 188 */ sb.append("<script type=\"text/javascript\">");
/* 189 */ sb.append("parent.KE.plugin[\"accessory\"].insert(\"").append(id).append("\",\"");
/* 190 */ sb.append(url).append("\",\"").append(title).append("\",\"").append(ext).append("\");</script>");
/* 191 */ sb.append("</body>").append("</html>");
/* 192 */ return sb.toString();
/* */ }
/* */
/* */ public String[] toArray(String filesType)
/* */ {
/* 204 */ if (filesType == null) {
/* 205 */ return null;
/* */ }
/* */
/* 208 */ String[] types = filesType.split(",");
/* 209 */ String[] allowTypes = new String[types.length];
/* 210 */ int i = 0;
/* 211 */ for (String type : types) {
/* 212 */ allowTypes[i] = type;
/* 213 */ i++;
/* */ }
/* */
/* 216 */ return allowTypes;
/* */ }
/* */ }