kindeditor富文本编辑器前端+后端实现

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qi8660306/article/details/53884048


效果图




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;
/*     */   }
/*     */ }


/* Location:           C:\Users\yanglei\Desktop\新建文件夹\kindeditorservlet\
 * Qualified Name:     com.elkan.kindeditor.upload.UploadAccessory
 * JD-Core Version:    0.6.0
 */


UploadImage.java

/*     */ package com.scrh.web.com.elkan.kindeditor;
/*     */ 
/*     */ import com.scrh.web.com.elkan.utils.ImageUtil;
/*     */ 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.FileUploadException;
/*     */ import org.apache.commons.fileupload.disk.DiskFileItemFactory;
/*     */ import org.apache.commons.fileupload.servlet.ServletFileUpload;
/*     */ 
/*     */ public class UploadImage extends HttpServlet
/*     */ {
/*     */   private static final long serialVersionUID = 5121794650920770832L;
/*  37 */   protected int MAX_WIDTH = -1;
/*     */ 
/*  39 */   protected int MAX_HEIGHT = -1;
/*     */ 
/*  41 */   protected long MAX_SIZE = 1000000L;
/*     */ 
/*  43 */   protected String[] IMAGETYPES = { "gif", "jpg", "jpeg", "png", "bmp" };
/*     */ 
/*  45 */   protected String UPLOAD_PATH = "";
/*     */ 
/*  48 */   protected String id = "";
/*     */ 
/*  50 */   protected String imgTitle = "";
/*     */ 
/*  52 */   protected int imgWidth = -1;
/*     */ 
/*  54 */   protected int imgHeight = -1;
/*     */ 
/*  56 */   protected String imgBorder = "";
/*     */ 
/*  58 */   protected String resizeImg = "";
/*     */ 
/*  60 */   protected boolean isFlag = false;
/*     */ 
/*  62 */   protected String tempTitle = "";
/*     */ 
/*     */   protected void doGet(HttpServletRequest request, HttpServletResponse response)
/*     */     throws ServletException, IOException
/*     */   {
/*  68 */     response.setContentType("text/html; charset=UTF-8");
/*  69 */     PrintWriter out = response.getWriter();
/*  70 */     String savePath = getInitParameter("UPLOAD_PATH");
/*  71 */     if ((savePath == null) || (savePath.isEmpty())) {
/*  72 */       out.println(alertMsg("你还没设置上传图片保存的目录路径!"));
/*  73 */       return;
/*     */     }
/*     */ 
/*  76 */     if (getInitParameter("MAX_SIZE") != null) {
/*  77 */       this.MAX_SIZE = Integer.parseInt(getInitParameter("MAX_SIZE"));
/*     */     }
/*     */ 
/*  80 */     if (getInitParameter("IMAGETYPES") != null) {
/*  81 */       this.IMAGETYPES = toArray(getInitParameter("IMAGETYPES"));
/*     */     }
/*     */ 
/*  84 */     String uploadPath = request.getSession().getServletContext().getRealPath("/") + savePath;
/*     */ 
/*  87 */     String saveUrl = request.getContextPath() + "/" + savePath;
/*     */ 
/*  90 */     if (!ServletFileUpload.isMultipartContent(request)) {
/*  91 */       out.println(alertMsg("请选择你要上传的图片!"));
/*  92 */       return;
/*     */     }
/*     */ 
/*  96 */     File uploadDir = new File(uploadPath);
/*  97 */     if (!uploadDir.isDirectory()) {
/*  98 */       out.println(alertMsg("上传图片保存的目录不存在。"));
/*  99 */       return;
/*     */     }
/*     */ 
/* 103 */     if (!uploadDir.canWrite()) {
/* 104 */       out.println(alertMsg("上传图片保存的目录没有写权限。"));
/* 105 */       return;
/*     */     }
/*     */ 
/* 109 */     FileItemFactory factory = new DiskFileItemFactory();
/* 110 */     ServletFileUpload upload = new ServletFileUpload(factory);
/* 111 */     upload.setHeaderEncoding("UTF-8");
/* 112 */     List items = null;
/* 113 */     String temp = null;
/*     */     try {
/* 115 */       items = upload.parseRequest(request);
/* 116 */       Iterator itr = items.iterator();
/* 117 */       while (itr.hasNext())
/*     */       {
/* 119 */         FileItem item = (FileItem)itr.next();
/*     */ 
/* 121 */         String fileName = item.getName();
/* 122 */         temp = item.getName();
/* 123 */         if ((temp != null) && (!this.isFlag)) {
/* 124 */           temp = temp.substring(temp.lastIndexOf("\\") + 1);
/* 125 */           this.tempTitle = temp;
/* 126 */           this.isFlag = true;
/*     */         }
/*     */ 
/* 129 */         if (item.getFieldName().equals("id")) {
/* 130 */           this.id = item.getString();
/*     */         }
/*     */ 
/* 134 */         if (item.getFieldName().equals("imgTitle")) {
/* 135 */           this.imgTitle = item.getString();
/* 136 */           if (this.imgTitle != null) {
/* 137 */             this.imgTitle = new String(this.imgTitle.getBytes("ISO8859-1"), "UTF-8");
/*     */           }
/*     */         }
/*     */ 
/* 141 */         if (item.getFieldName().equals("imgWidth")) {
/* 142 */           String imgWidth = item.getString();
/* 143 */           if ((imgWidth != null) && (!imgWidth.isEmpty())) {
/* 144 */             this.imgWidth = Integer.parseInt(imgWidth);
/*     */           }
/*     */         }
/*     */ 
/* 148 */         if (item.getFieldName().equals("imgHeight")) {
/* 149 */           String imgHeight = item.getString();
/* 150 */           if ((imgHeight != null) && (!imgHeight.isEmpty())) {
/* 151 */             this.imgHeight = Integer.parseInt(imgHeight);
/*     */           }
/*     */         }
/*     */ 
/* 155 */         if (item.getFieldName().equals("imgBorder")) {
/* 156 */           this.imgBorder = item.getString();
/*     */         }
/*     */ 
/* 159 */         long fileSize = item.getSize();
/* 160 */         if (item.isFormField())
/*     */           continue;
/* 162 */         if (fileSize > this.MAX_SIZE) {
/* 163 */           out.println(alertMsg("上传文件大小超过限制。"));
/* 164 */           return;
/*     */         }
/*     */ 
/* 168 */         String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
/* 169 */         if (!Arrays.asList(this.IMAGETYPES).contains(fileExt)) {
/* 170 */           out.println(alertMsg("上传图片扩展名是不允许的扩展名。"));
/* 171 */           return;
/*     */         }
/*     */ 
/* 174 */         SimpleDateFormat folderNameFormat = new SimpleDateFormat("yyyyMMdd");
/* 175 */         String realPath = uploadPath + folderNameFormat.format(new Date());
/* 176 */         File folder = new File(realPath);
/* 177 */         boolean flag = folder.exists();
/*     */ 
/* 179 */         if (!flag) {
/* 180 */           flag = folder.mkdir();
/*     */         }
/*     */ 
/* 183 */         if (flag) {
/* 184 */           SimpleDateFormat fileNameFormat = new SimpleDateFormat("yyyyMMddHHmmss");
/* 185 */           String newFileName = fileNameFormat.format(new Date()) + "_" + new Random().nextInt(1000) + "." + fileExt;
/* 186 */           File uploadedFile = new File(realPath, newFileName);
/* 187 */           item.write(uploadedFile);
/* 188 */           this.resizeImg = uploadedFile.getPath();
/* 189 */           this.resizeImg = this.resizeImg.replaceAll("\\\\", "/");
/* 190 */           saveUrl = saveUrl + folderNameFormat.format(new Date()) + "/" + newFileName;
/*     */         } else {
/* 192 */           System.out.println(" 文件夹创建失败,请确认磁盘没有写保护并且空件足够");
/*     */         }
/*     */ 
/*     */       }
/*     */ 
/* 198 */       String max_width = getInitParameter("MAX_WIDTH");
/* 199 */       String max_height = getInitParameter("MAX_HEIGHT");
/* 200 */       if ((max_width != null) && (!max_width.isEmpty())) {
/* 201 */         this.MAX_WIDTH = Integer.parseInt(max_width);
/*     */       }
/* 203 */       if ((max_height != null) && (!max_height.isEmpty())) {
/* 204 */         this.MAX_HEIGHT = Integer.parseInt(max_height);
/*     */       }
/*     */ 
/* 207 */       if ((this.imgTitle == null) || (this.imgTitle.isEmpty())) {
/* 208 */         this.imgTitle = this.tempTitle;
/*     */       }
/*     */ 
/* 212 */       if ((this.MAX_WIDTH != -1) || (this.MAX_HEIGHT != -1))
/*     */       {
/* 214 */         ImageUtil.resizeImg(this.resizeImg, this.resizeImg, this.MAX_WIDTH, this.MAX_HEIGHT);
/*     */ 
/* 216 */         if (this.imgWidth > ImageUtil.ImgWidth) {
/* 217 */           this.imgWidth = ImageUtil.ImgWidth;
/*     */         }
/*     */ 
/* 220 */         if (this.imgHeight > ImageUtil.ImgHeight) {
/* 221 */           this.imgHeight = ImageUtil.ImgHeight;
/*     */         }
/*     */ 
/* 225 */         out.println(insertEditor(this.id, saveUrl, this.imgTitle, this.imgWidth, this.imgHeight, this.imgBorder));
/*     */       }
/*     */       else {
/* 228 */         out.println(insertEditor(this.id, saveUrl, this.imgTitle, this.imgWidth, this.imgHeight, this.imgBorder));
/*     */       }
/*     */     }
/*     */     catch (FileUploadException e) {
/* 232 */       e.printStackTrace();
/*     */     } catch (Exception e) {
/* 234 */       e.printStackTrace();
/*     */     } finally {
/* 236 */       out.flush();
/* 237 */       out.close();
/* 238 */       this.isFlag = false;
/*     */     }
/*     */   }
/*     */ 
/*     */   protected void doPost(HttpServletRequest request, HttpServletResponse response)
/*     */     throws ServletException, IOException
/*     */   {
/* 246 */     doGet(request, response);
/*     */   }
/*     */ 
/*     */   public String alertMsg(String message)
/*     */   {
/* 257 */     StringBuffer sb = new StringBuffer("{\"error\":\"1\",\"message\":\"");
/* 258 */     sb.append(message).append("\"}");
/* 259 */     return sb.toString();
/*     */   }
/*     */ 
/*     */   public String insertEditor(String id, String saveUrl, String imgTitle, int imgWidth, int imgHeight, String imgBorder)
/*     */   {
/* 281 */     StringBuffer sb = new StringBuffer("<script type\"text/javascript\">");
/* 282 */     sb.append("parent.KE.plugin[\"image\"].insert(\"").append(id).append("\",\"");
/* 283 */     sb.append(saveUrl).append("\",\"").append(imgTitle).append("\",\"");
/* 284 */     sb.append(imgWidth).append("\",\"").append(imgHeight).append("\",\"");
/* 285 */     sb.append(imgBorder).append("\");");
/* 286 */     sb.append("</script>");
/* 287 */     return sb.toString();
/*     */   }
/*     */ 
/*     */   public String[] toArray(String filesType)
/*     */   {
/* 299 */     if (filesType == null) {
/* 300 */       return null;
/*     */     }
/*     */ 
/* 303 */     String[] types = filesType.split(",");
/* 304 */     String[] allowTypes = new String[types.length];
/* 305 */     int i = 0;
/* 306 */     for (String type : types) {
/* 307 */       allowTypes[i] = type;
/* 308 */       i++;
/*     */     }
/*     */ 
/* 311 */     return allowTypes;
/*     */   }
/*     */ }


/* Location:           C:\Users\yanglei\Desktop\新建文件夹\kindeditorservlet\
 * Qualified Name:     com.elkan.kindeditor.upload.UploadImage
 * JD-Core Version:    0.6.0
 */

UploadImageManager.java

/*     */ package com.scrh.web.com.elkan.kindeditor;
/*     */ 
/*     */ import java.io.File;
/*     */ import java.io.IOException;
/*     */ import java.io.PrintWriter;
/*     */ import java.text.SimpleDateFormat;
/*     */ import java.util.ArrayList;
/*     */ import java.util.Arrays;
/*     */ import java.util.Collections;
/*     */ import java.util.Comparator;
/*     */ import java.util.Hashtable;
/*     */ import java.util.List;
/*     */ 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;
/*     */ 
/*     */ public class UploadImageManager extends HttpServlet
/*     */ {
/*     */   private static final long serialVersionUID = -8359652838938248988L;
/*  23 */   protected String[] FILETYPES = { "gif", "jpg", "jpeg", "png", "bmp" };
/*     */ 
/*  25 */   protected String UPLOAD_PATH = "";
/*     */ 
/*     */   public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
/*     */   {
/*  29 */     response.setContentType("text/html; charset=UTF-8");
/*  30 */     PrintWriter out = response.getWriter();
/*  31 */     String savePath = getInitParameter("UPLOAD_PATH");
/*  32 */     if ((savePath == null) || (savePath.isEmpty())) {
/*  33 */       out.println(alertMsg("你还没设置读取上传图片保存的目录路径!"));
/*  34 */       return;
/*     */     }
/*     */ 
/*  37 */     String rootPath = request.getSession().getServletContext().getRealPath("/") + savePath;
/*     */ 
/*  39 */     String rootUrl = request.getContextPath() + "/" + savePath;
/*     */ 
/*  41 */     String path = request.getParameter("path") != null ? request.getParameter("path") : "";
/*  42 */     String currentPath = rootPath + path;
/*  43 */     String currentUrl = rootUrl + path;
/*  44 */     String currentDirPath = path;
/*  45 */     String moveupDirPath = "";
/*     */ 
/*  47 */     if (!"".equals(path)) {
/*  48 */       String str = currentDirPath.substring(0, currentDirPath.length() - 1);
/*  49 */       moveupDirPath = str.lastIndexOf("/") >= 0 ? str.substring(0, str.lastIndexOf("/") + 1) : "";
/*     */     }
/*     */ 
/*  53 */     String order = request.getParameter("order") != null ? request.getParameter("order").toLowerCase() : "name";
/*     */ 
/*  56 */     if (path.indexOf("..") >= 0) {
/*  57 */       out.println(alertMsg("不允许使用移动到上一级目录"));
/*  58 */       return;
/*     */     }
/*     */ 
/*  62 */     if ((!"".equals(path)) && (!path.endsWith("/"))) {
/*  63 */       out.println("Parameter is not valid.");
/*  64 */       return;
/*     */     }
/*     */ 
/*  67 */     File currentPathFile = new File(currentPath);
/*  68 */     if (!currentPathFile.isDirectory()) {
/*  69 */       out.println("Directory does not exist.");
/*  70 */       return;
/*     */     }
/*     */ 
/*  74 */     List fileList = new ArrayList();
/*  75 */     if (currentPathFile.listFiles() != null) {
/*  76 */       for (File file : currentPathFile.listFiles()) {
/*  77 */         Hashtable hash = new Hashtable();
/*  78 */         String fileName = file.getName();
/*  79 */         if (file.isDirectory()) {
/*  80 */           hash.put("is_dir", Boolean.valueOf(true));
/*  81 */           hash.put("has_file", Boolean.valueOf(file.listFiles() != null));
/*  82 */           hash.put("filesize", Long.valueOf(0L));
/*  83 */           hash.put("is_photo", Boolean.valueOf(false));
/*  84 */           hash.put("filetype", "");
/*  85 */         } else if (file.isFile()) {
/*  86 */           String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
/*  87 */           hash.put("is_dir", Boolean.valueOf(false));
/*  88 */           hash.put("has_file", Boolean.valueOf(false));
/*  89 */           hash.put("filesize", Long.valueOf(file.length()));
/*  90 */           hash.put("is_photo", Boolean.valueOf(Arrays.asList(this.FILETYPES).contains(fileExt)));
/*  91 */           hash.put("filetype", fileExt);
/*     */         }
/*  93 */         hash.put("filename", fileName);
/*  94 */         hash.put("datetime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Long.valueOf(file.lastModified())));
/*  95 */         fileList.add(hash);
/*     */       }
/*     */     }
/*     */ 
/*  99 */     if ("size".equals(order))
/* 100 */       Collections.sort(fileList, new SizeComparator());
/* 101 */     else if ("type".equals(order))
/* 102 */       Collections.sort(fileList, new TypeComparator());
/*     */     else {
/* 104 */       Collections.sort(fileList, new NameComparator());
/*     */     }
/*     */ 
/* 107 */     out.println(toJSONString(currentUrl, currentDirPath, moveupDirPath, fileList));
/*     */ 
/* 109 */     out.flush();
/* 110 */     out.close();
/*     */   }
/*     */ 
/*     */   public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
/*     */   {
/* 115 */     doGet(request, response);
/*     */   }
/*     */ 
/*     */   public String alertMsg(String message)
/*     */   {
/* 126 */     StringBuffer sb = new StringBuffer("<script type\"text/javascript\">");
/* 127 */     sb.append("alert(\"").append(message).append("\");");
/* 128 */     sb.append("</script>");
/* 129 */     return sb.toString();
/*     */   }
/*     */ 
/*     */   public String toJSONString(String currentUrl, String currentDirPath, String moveupDirPath, List<Hashtable<?, ?>> fileList) {
/* 133 */     StringBuilder sb = new StringBuilder("{\"current_url\":\"");
/* 134 */     sb.append(currentUrl).append("\",").append("\"current_dir_path\":\"");
/* 135 */     sb.append(currentDirPath).append("\",\"moveup_dir_path\":\"").append(moveupDirPath).append("\",");
/* 136 */     sb.append("\"file_list\":[");
/* 137 */     int i = 0;
/* 138 */     sb.append("{");
/* 139 */     for (Hashtable he : fileList) {
/* 140 */       if (i != fileList.size() - 1) {
/* 141 */         sb.append("\"filename\":\"").append(he.get("filename")).append("\",");
/* 142 */         sb.append("\"filesize\":").append(he.get("filesize")).append(",");
/* 143 */         sb.append("\"filetype\":\"").append(he.get("filetype")).append("\",");
/* 144 */         sb.append("\"has_file\":").append(he.get("has_file")).append(",");
/* 145 */         sb.append("\"is_dir\":").append(he.get("is_dir")).append(",");
/* 146 */         sb.append("\"is_photo\":").append(he.get("is_photo")).append(",");
/* 147 */         sb.append("\"datetime\":\"").append(he.get("datetime")).append("\"");
/* 148 */         sb.append("},{");
/*     */       } else {
/* 150 */         sb.append("\"filename\":\"").append(he.get("filename")).append("\",");
/* 151 */         sb.append("\"filesize\":").append(he.get("filesize")).append(",");
/* 152 */         sb.append("\"filetype\":\"").append(he.get("filetype")).append("\",");
/* 153 */         sb.append("\"has_file\":").append(he.get("has_file")).append(",");
/* 154 */         sb.append("\"is_dir\":").append(he.get("is_dir")).append(",");
/* 155 */         sb.append("\"is_photo\":").append(he.get("is_photo")).append(",");
/* 156 */         sb.append("\"datetime\":\"").append(he.get("datetime")).append("\"");
/* 157 */         sb.append("}");
/*     */       }
/* 159 */       i++;
/*     */     }
/* 161 */     i = 0;
/* 162 */     sb.append("],\"total_count\":").append(fileList.size()).append("}");
/* 163 */     return sb.toString();
/*     */   }
/*     */   public class NameComparator implements Comparator<Object> {
/*     */     public NameComparator() {
/*     */     }
/* 168 */     public int compare(Object a, Object b) { Hashtable hashA = (Hashtable)a;
/* 169 */       Hashtable hashB = (Hashtable)b;
/* 170 */       if ((((Boolean)hashA.get("is_dir")).booleanValue()) && 
/* 171 */         (!((Boolean)hashB.get("is_dir")).booleanValue()))
/* 172 */         return -1;
/* 173 */       if ((!((Boolean)hashA.get("is_dir")).booleanValue()) && 
/* 174 */         (((Boolean)hashB.get("is_dir")).booleanValue())) {
/* 175 */         return 1;
/*     */       }
/* 177 */       return ((String)hashA.get("filename"))
/* 178 */         .compareTo((String)hashB.get("filename")); } 
/*     */   }
/*     */ 
/*     */   public class SizeComparator implements Comparator<Object> {
/*     */     public SizeComparator() {
/*     */     }
/*     */ 
/*     */     public int compare(Object a, Object b) {
/* 185 */       Hashtable hashA = (Hashtable)a;
/* 186 */       Hashtable hashB = (Hashtable)b;
/* 187 */       if ((((Boolean)hashA.get("is_dir")).booleanValue()) && 
/* 188 */         (!((Boolean)hashB.get("is_dir")).booleanValue()))
/* 189 */         return -1;
/* 190 */       if ((!((Boolean)hashA.get("is_dir")).booleanValue()) && 
/* 191 */         (((Boolean)hashB.get("is_dir")).booleanValue())) {
/* 192 */         return 1;
/*     */       }
/* 194 */       if (((Long)hashA.get("filesize")).longValue() > 
/* 195 */         ((Long)hashB.get("filesize")).longValue())
/*     */       {
/* 196 */         return 1;
/* 197 */       }if (((Long)hashA.get("filesize")).longValue() < 
/* 198 */         ((Long)hashB.get("filesize")).longValue())
/*     */       {
/* 199 */         return -1;
/*     */       }
/* 201 */       return 0;
/*     */     }
/*     */   }
/*     */ 
/*     */   public class TypeComparator implements Comparator<Object> {
/*     */     public TypeComparator() {
/*     */     }
/*     */ 
/*     */     public int compare(Object a, Object b) {
/* 209 */       Hashtable hashA = (Hashtable)a;
/* 210 */       Hashtable hashB = (Hashtable)b;
/* 211 */       if ((((Boolean)hashA.get("is_dir")).booleanValue()) && 
/* 212 */         (!((Boolean)hashB.get("is_dir")).booleanValue()))
/* 213 */         return -1;
/* 214 */       if ((!((Boolean)hashA.get("is_dir")).booleanValue()) && 
/* 215 */         (((Boolean)hashB.get("is_dir")).booleanValue())) {
/* 216 */         return 1;
/*     */       }
/* 218 */       return ((String)hashA.get("filetype"))
/* 219 */         .compareTo((String)hashB.get("filetype"));
/*     */     }
/*     */   }
/*     */ }


/* Location:           C:\Users\yanglei\Desktop\新建文件夹\kindeditorservlet\
 * Qualified Name:     com.elkan.kindeditor.upload.UploadImageManager
 * JD-Core Version:    0.6.0
 */


utils包

ImageUtil.java

/*    */ package com.scrh.web.com.elkan.utils;
/*    */ 
/*    */ import com.sun.image.codec.jpeg.JPEGCodec;
/*    */ import com.sun.image.codec.jpeg.JPEGImageEncoder;
/*    */ import java.awt.Graphics;
/*    */ import java.awt.Image;
/*    */ import java.awt.image.BufferedImage;
/*    */ import java.io.File;
/*    */ import java.io.FileOutputStream;
/*    */ import java.io.IOException;
/*    */ import javax.imageio.ImageIO;
/*    */ 
/*    */ public class ImageUtil
/*    */ {
/* 21 */   public static int ImgWidth = -1;
/*    */ 
/* 23 */   public static int ImgHeight = -1;
/*    */ 
/*    */   static {
/* 26 */     System.setProperty("jmagick.systemclassloader", "no");
/*    */   }
/*    */ 
/*    */   public static void resizeImg(String imgsrc, String imgdist, int widthdist, int heightdist)
/*    */   {
/*    */     try
/*    */     {
/* 44 */       File srcfile = new File(imgsrc);
/* 45 */       if (!srcfile.exists()) {
/* 46 */         return;
/*    */       }
/* 48 */       Image src = ImageIO.read(srcfile);
/* 49 */       ImgWidth = src.getWidth(null);
/* 50 */       ImgHeight = src.getHeight(null);
/* 51 */       if (ImgWidth < widthdist)
/* 52 */         widthdist = ImgWidth;
/*    */       else {
/* 54 */         ImgWidth = widthdist;
/*    */       }
/* 56 */       if (ImgHeight < heightdist)
/* 57 */         heightdist = ImgHeight;
/*    */       else {
/* 59 */         ImgHeight = heightdist;
/*    */       }
/* 61 */       BufferedImage tag = new BufferedImage(widthdist, heightdist, 1);
/*    */ 
/* 63 */       tag.getGraphics().drawImage(src.getScaledInstance(widthdist, heightdist, 4), 0, 0, null);
/* 64 */       FileOutputStream out = new FileOutputStream(imgdist);
/* 65 */       JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
/* 66 */       encoder.encode(tag);
/* 67 */       out.close();
/*    */     } catch (IOException ex) {
/* 69 */       ex.printStackTrace();
/*    */     }
/*    */   }
/*    */ }


/* Location:           C:\Users\yanglei\Desktop\新建文件夹\kindeditorservlet\
 * Qualified Name:     com.elkan.utils.ImageUtil
 * JD-Core Version:    0.6.0
 */

web.xml

<!-- 图片上传的SERVLET -->
<servlet>
<servlet-name>UploadImage</servlet-name>
<servlet-class>com.elkan.kindeditor.upload.UploadImage</servlet-class>
<!-- 上传图片保存的目录 -->
<init-param>
<param-name>UPLOAD_PATH</param-name>
<param-value>uploadImg/</param-value>
</init-param>
<!-- 限制上传图片的大小,单位字节(缺省值为1MB) -->
<init-param>
<param-name>MAX_SIZE</param-name>
<param-value>1024000</param-value>
</init-param>
<init-param>
<!-- 上传图片的类型(缺省值为gif, jpg, jpeg, png, bmp) -->
<param-name>IMAGETYPES</param-name>
<param-value>jpg,png,bmp,jpeg,gif</param-value>
</init-param>
<!-- 上传图片的宽度,大于此宽度时图片会被压缩(缺省为不限定) -->
<init-param>
<param-name>MAX_WIDTH</param-name>
<param-value>500</param-value>
</init-param>
<!-- 上传图片的高度,大于此高度时图片会被压缩(缺省为不限定) -->
<init-param>
<param-name>MAX_HEIGHT</param-name>
<param-value>500</param-value>
</init-param>
</servlet>
<!-- 图片上传管理的SERVLET -->
<servlet>
<servlet-name>UploadImageManager</servlet-name>
<servlet-class>com.elkan.kindeditor.upload.UploadImageManager</servlet-class>
<!-- 上传图片保存的目录 -->
<init-param>
<param-name>UPLOAD_PATH</param-name>
<param-value>uploadImg/</param-value>
</init-param>
</servlet>
<!-- 附件上传的SERVLET -->
<servlet>
<servlet-name>UploadAccessory</servlet-name>
<servlet-class>com.elkan.kindeditor.upload.UploadAccessory</servlet-class>
<!-- 上传附件保存的目录 -->
<init-param>
<param-name>UPLOAD_PATH</param-name>
<param-value>uploadAttach/</param-value>
</init-param>
<!-- 上传附件的大小,单位字节(缺省为1MB) -->
<init-param>
<param-name>MAX_SIZE</param-name>
<param-value>1000000</param-value>
</init-param>
<!-- 上传文件的类型(缺省为doc, xls, ppt, pdf, txt, rar, zip) -->
<init-param>
<param-name>FILETYPES</param-name>
<param-value>doc,xls,ppt,zip,rar,txt</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>UploadImage</servlet-name>
<url-pattern>/uploadImage.html</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>UploadImageManager</servlet-name>
<url-pattern>/uploadImgManager.html</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>UploadAccessory</servlet-name>
<url-pattern>/uploadAccessory.html</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<login-config>
<auth-method>BASIC</auth-method>
</login-config>

editor.jsp富文本输入文件

<%@page contentType="text/html" language="java" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  <%@ include file="/comm.jsp"%>
    <title>KindEditor新闻新增编辑器</title>
<script type="text/javascript" src="${pageContext.request.contextPath}/kindeditor/kindeditor-min.js" charset="UTF-8"></script>
<script charset="utf-8" src="${ctx}/report/editor.js"></script>
<script type="text/javascript">
KE.show({
id : "editor",
    width : "840px",
    height : "480px",   
    resizeMode : 1,
    allowFileManager : true,
    /*图片上传的SERVLET路径*/
    imageUploadJson : "${pageContext.request.contextPath}/uploadImage.html", 
    /*图片管理的SERVLET路径*/     
    fileManagerJson : "${pageContext.request.contextPath}/uploadImgManager.html",
    /*允许上传的附件类型*/
    accessoryTypes : "doc|xls|pdf|txt|ppt|rar|zip",
    /*附件上传的SERVLET路径*/
    accessoryUploadJson : "${pageContext.request.contextPath}/uploadAccessory.html"
});
</script>
  </head>
  
  <body bgcolor="#EEF2FB" style="width: 100%;">
  <div style="height: auto;  padding-left: 2%;">
  <div>
  <form  id="ff" name="ff" method="post">
<h3>KindEditor新增新闻编辑器</h3>
<tr>
          <td>新闻标题</td>
          <td><input type="text" id="newstitle" name="newstitle" /></td>
          </tr>
          <tr>
          <td>新闻类型</td>
          <td>
             <select id="newstype" name="newstype">
              <option>国内新闻</option>
              <option>国际新闻</option>
              <option>体育新闻</option>
              <option>娱乐新闻</option>
            </select>
           </td>
           </tr>
          <tr>
          <td>新闻来源</td>
          <td><input type="text" id="souce" name="souce" /></td>
          </tr>
          <tr>
          <td>新闻作者</td>
          <td><input type="text" id="author" name="author" /></td>
          </tr>
<textarea id="editor" name="editor" rows="" cols="" style="width:840px;height:600px;visibility:hidden;">
</textarea>
<input type="submit" name="submit" value="输出内容" οnclick="submitForm()" style="cursor: pointer;">
</form>
  </div>
  </div>
  </body>
</html>

editor.jsp富文本再输入文件

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
request.setCharacterEncoding("UTF-8");
String htmlData = request.getParameter("content1") != null ? request.getParameter("content1") : "";
%>
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<%@ include file="/comm.jsp"%>
<title>警方新闻(新增/修改)</title>
<link rel="stylesheet" href="${ctx}/kindeditor/themes/default/default.css" />
<link rel="stylesheet" href="${ctx}/kindeditor/plugins/code/prettify.css" />
<script charset="utf-8" src="${ctx}/kindeditor/kindeditor.js"></script>
<script charset="utf-8" src="${ctx}/kindeditor/lang/zh_CN.js"></script>
<script charset="utf-8" src="${ctx}/kindeditor/plugins/code/prettify.js"></script>
<script>
KindEditor.ready(function(K) {
var editor1 = K.create('textarea[name="content1"]', {
cssPath : 'kindeditor/plugins/code/prettify.css',
uploadJson : 'kindeditor/jsp/upload_json.jsp',
fileManagerJson : 'kindeditor/jsp/file_manager_json.jsp',
allowFileManager : true,
afterCreate : function() {
var self = this;
K.ctrl(document, 13, function() {
self.sync();
document.forms['example'].submit();
});
K.ctrl(self.edit.doc, 13, function() {
self.sync();
document.forms['example'].submit();
});
}
});
prettyPrint();
});
</script>
</head>
<body>
<%=htmlData%>
<form name="example" method="post" action="demo.jsp">
<div align="center">
<table cellpadding="5" class="tb2" width="100%">
<tr>
<h4>文章编辑</h4>
           <table>
        <tr>
          <td>标题</td>
          <td><input type="text" name="news.title" /></td>
          <td>作者</td>
          <td><input type="text" name="news.author" /></td>
          <td>类型</td>
          <td>
             <select name="news.types">
              <option>国内新闻</option>
              <option>国际新闻</option>
              <option>体育新闻</option>
              <option>娱乐新闻</option>
            </select>
           </td>
          <td>发布单位</td>
          <td>
             <select name="news.department">
              <option>办公室</option>
              <option>宣传部</option>
              <option>文艺团</option>
              <option>工会</option>
            </select>
           </td>
        </tr>
      </table>
</tr>
<textarea name="content1" cols="100" rows="8" style="width:900px;height:300px;visibility:hidden;"><%=htmlspecialchars(htmlData)%></textarea>
<br />
<input type="submit" name="button" value="提交内容" /> (提交快捷键: Ctrl + Enter)
</table>
</div>
</form>
</body>
</html>
<%!
private String htmlspecialchars(String str) {
str = str.replaceAll("&", "&amp;");
str = str.replaceAll("<", "&lt;");
str = str.replaceAll(">", "&gt;");
str = str.replaceAll("\"", "&quot;");
return str;
}
%>



展开阅读全文

没有更多推荐了,返回首页