1449  
查询码: 00000363
KindEditor ctrl+v添加图片功能 (支持火狐和chrome)
作者: 系统管理员 于 2018年08月21日 发布在分类 / 二次开发 ,于 2018年08月21日 编辑
import string 图片 resultvo kindeditor imgagefilepath common imagedatabase constants logger


浏览器端DEMO

<%@ 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" />
<title>KindEditor JSP</title>
<link rel="stylesheet" href="../themes/default/default.css" />
<link rel="stylesheet" href="../plugins/code/prettify.css" />
<script charset="utf-8" src="jquery-1.11.3.min.js"></script>
<script charset="utf-8" src="../kindeditor-all.js"></script>
<script charset="utf-8" src="../lang/zh-CN.js"></script>
<script charset="utf-8" src="../plugins/code/prettify.js"></script>
<script>
	KindEditor.ready(function(K) {
		var editor1 = K.create('textarea[name="content1"]', {
			cssPath : '../plugins/code/prettify.css',
			uploadJson : '../jsp/upload_json.jsp',
			fileManagerJson : '../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();
				});
				var doc = this.edit.doc;
				var cmd = this.edit.cmd;
				$(doc.body).bind('paste', function(ev) {
					var $this = $(this);
					var dataItem = ev.originalEvent.clipboardData.items[0];
					if (dataItem) {
						var file = dataItem.getAsFile();
						if (file) {
							var reader = new FileReader();
							reader.onload = function(evt) {
								var imageDataBase64 = evt.target.result;
								$.post('../jsp/file_manager_json.jsp',{},function(json){
									cmd.inserthtml(json);
								});
							};
							var event = ev || window.event;//兼容IE
							//取消事件相关的默认行为
							if (event.preventDefault) //标准技术
							{
								event.preventDefault();
							}
							if (event.returnValue) //兼容IE9之前的IE
							{
								event.returnValue = false;
							}
							reader.readAsDataURL(file);
						}
					}
				});
			}
		});
		prettyPrint();
	});
</script>
</head>
<body>
	<%=htmlData%>
	<form name="example" method="post" action="demo.jsp">
		<textarea name="content1" cols="100" rows="8"
			style="width: 700px; height: 200px; visibility: hidden;"><%=htmlspecialchars(htmlData)%></textarea>
		<br /> <input type="submit" name="button" value="提交内容" /> (提交快捷键:
		Ctrl + Enter)
	</form>
</body>
</html>
<%!private String htmlspecialchars(String str) {
		str = str.replaceAll("&", "&");
		str = str.replaceAll("<", "<");
		str = str.replaceAll(">", ">");
		str = str.replaceAll("\"", """);
		return str;
	}%>

java 后端图片解析DEMO

public void imageUploadBase64(HttpServletResponse response) {
    @SuppressWarnings("unchecked")
    ResultVo<String> resultVo = ResultVo.getInance(); try{
      String imgageFilePath = null;
      String imageDataBase64 = getRequest().getParameter("imageDataBase64"); if(!StringUtil.isEmpty(imageDataBase64)){
         String[] arrImageData = imageDataBase64.split(",");
         String[] arrTypes = arrImageData[0].split(";");
         String[] arrImageType = arrTypes[0].split(":");
         String imageType = arrImageType[1];
         String imageTypeSuffix = imageType.split("/")[1]; if("base64".equalsIgnoreCase(arrTypes[1])&&this.IMAGETYPES.indexOf(imageTypeSuffix.toLowerCase())!=-1){
           BASE64Decoder decoder = new BASE64Decoder(); byte[] decodeBuffer = decoder.decodeBuffer(arrImageData[1]);
           SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
           String currFormat = df.format(new Date());
           File currFileBag = new File(Constants.ATTACHED_PATH +currFormat); if(!currFileBag.exists()){
             currFileBag.mkdirs();
           }
           imgageFilePath = currFormat+"/"+new Random().nextInt(100000) + "." + imageTypeSuffix;
           File currFile = new File(Constants.ATTACHED_PATH +imgageFilePath); int i = 0; while(currFile.exists()){
             imgageFilePath = currFormat+"/"+new Random().nextInt(100000) + "." + imageTypeSuffix;
             currFile = new File(Constants.ATTACHED_PATH +imgageFilePath);
             i++; if(i>=100000){
               imgageFilePath = null;
               currFile = null; break;
             }
           } if(currFile!=null){
             OutputStream out = new FileOutputStream(currFile);
             out.write(decodeBuffer); 
             out.flush(); 
             out.close(); 
           }
         }
       } //imgageFilePath路径存在表示上传成功 if(imgageFilePath!=null){
         resultVo.setResult(Constants.ATTACHED_URL +imgageFilePath);
       }else{
        logger.error("上传图片发生未知异常....");
        resultVo.setErrCode(ErrorCode.SYS_ERROR_COMMON_CODE);
        resultVo.setErrMsg(ErrorCode.SYS_ERROR_COMMON_MSG); 
       }
    }catch(Exception e){
      logger.error("上传图片发生异常: ", e);
      resultVo.setErrCode(ErrorCode.SYS_ERROR_COMMON_CODE);
      resultVo.setErrMsg(ErrorCode.SYS_ERROR_COMMON_MSG);
    }
     outJSONData(resultVo,response);
  }
0人参与



 历史版本

备注 修改日期 修改人
格式调整 2018-08-21 12:36:49[当前版本] 系统管理员
格式调整 2018-08-21 12:35:29 系统管理员
格式调整 2018-08-21 12:35:07 系统管理员
格式调整 2018-08-21 12:31:07 系统管理员

wcp知识库系统-京ICP备15024440号-1 -V 5.2.0 -wcp