java常用的编辑器之kindeditor

先上图,没图差不多就看不下去的,我懂!

来个毛爷的,看着好像就能发财一样。。。。

java常用的编辑器之kindeditor_第1张图片


首先下载必要的文件和jar包:http://download.csdn.net/detail/xb12369/9516909


代码的结构:



1. 【资源文件】解压zip文件,将editor复制到项目的的webapps目录下。


2. 【jar包】将kindeditor/jsp/lib目录下的3个jar文件复制到Tomcat的lib目录下,并重新启动Tomcat。
* commons-fileupload-1.2.1.jar
* commons-io-1.4.jar
* json_simple-1.1.jar


3. 配置路径:

//文件保存目录路径
//String savePath = pageContext.getServletContext().getRealPath("/") + "attached/";
String savePath = (String)request.getAttribute("tempDir");


//文件保存目录URL
//String saveUrl  = request.getContextPath() + "/attached/";
String saveUrl  = request.getContextPath() + (String)request.getAttribute("imgDir");


这里有个说明:如果request.getAttribute不作类型转换的话,会报错的!即(String)request.getAttribute("imgDir");



4,根据自己对应的上传方式修改对应的上传部分的代码:

这是我的上传代码:

/*****************开始改造成自己的上传方法************************************/
DefaultMultipartHttpServletRequest multipartRequest = (DefaultMultipartHttpServletRequest)request;
if (multipartRequest != null) {
	Iterator<String> itr = multipartRequest.getFileNames();
	while (itr.hasNext()) {
		MultipartFile img = multipartRequest.getFile(itr.next().toString());
		
		String fileName = img.getOriginalFilename();
		
		//检查扩展名
		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{
			InputStream in = img.getInputStream();
			
			FileOutputStream fs = new FileOutputStream(savePath+newFileName);  
			
			byte[] buffer = new byte[1024 * 1024];
			int byteread = 0;  
			while ((byteread = in.read(buffer)) != -1) {
				fs.write(buffer, 0, byteread);  
			}  
			fs.close();  
			in.close(); 
		}catch(Exception e){
			out.println(getError("上传文件失败。"));
			return;
		} 

		JSONObject obj = new JSONObject();
		obj.put("error", 0);
		obj.put("url", saveUrl + newFileName);
		out.println(obj.toJSONString());
	}

提供的上传方式:

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());
	}
}

5、空格处理,如果不对某些特殊符号做特殊处理的话,会导致html代码丢失

存储的时候:

private String htmlspecialchars(String str) {
		str = str.replaceAll("&", "&");
		str = str.replaceAll("<", "<");
		str = str.replaceAll(">", ">");
		str = str.replaceAll("\"", """);
		return str;
	}
content = htmlspecialchars(content);
				if(!StringUtils.isEmpty(id)){
					Map<String, Object> map = new HashMap<String, Object>();
					map.put("content", content);
					
					this.activityInfoService.modify(id, map);
				}

取出来的时候:

private String htmlspecialchars(String str) {
		str = str.replaceAll("&", "&");
		str = str.replaceAll("<", "<");
		str = str.replaceAll(">", ">");
		str = str.replaceAll(""", "\"");
		//图片懒加载处理
		//<img  class="scrollLoading" data-url="http://7xlv3q.com2.z0.glb.qiniucdn.com${item.img }" src="<%=request.getContextPath()%>/easyui/app_h5/activity/images/grey.gif" >
		//<img src="/imgs/image/20160511/20160511162627_319.jpg" alt="" /> 
		str = str.replaceAll("src=", "data-url=").replaceAll("alt=\"\"", "class=\"scrollLoading\" src=\"<%=request.getContextPath()%>/easyui/app_h5/activity/images/grey.gif\"");
		return str;
	}

request.setAttribute("content", htmlspecialchars(info.getContent()));

good luck~


你可能感兴趣的:(编辑器kindeditor)