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

本文介绍了如何在Java环境中使用KindEditor实现前端和后端的文件上传功能,包括图片、附件等。文章详细讲解了UploadImage和UploadAccessory两个Servlet的实现,用于处理文件上传,并提供了相关配置如`web.xml`和`editor.jsp`的代码示例。通过这个教程,读者可以了解到如何将KindEditor集成到自己的项目中,实现富文本编辑和文件管理功能。
摘要由CSDN通过智能技术生成


效果图




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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值