此案例 demo 为 SpringBoot 开发

1、官网下载相关资源包:http://kindeditor.net/down.php

2、编写页面(引入相关JS)

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>kindeditor富文本编辑器</title>
<link href="/kindeditor-4-1-10/themes/default/default.css" type="text/css" rel="stylesheet"> <script type="text/javascript" charset="utf-8" src="/jquery-3.2.0.min.js"></script>
<script type="text/javascript" charset="utf-8" src="/kindeditor-4-1-10/kindeditor-all-min.js"></script>
<script type="text/javascript" charset="utf-8" src="/kindeditor-4-1-10/lang/zh_CN.js"></script>
<script type="text/javascript" charset="utf-8" src="/kindeditor-Own/kindeditor.js"></script> </head>
<body>
  <textarea id="itemAddForm" style="width:800px;height:300px;visibility:hidden;" name="desc"></textarea>
</body>
</html>

3、编写 JS(/kindeditor-Own/kindeditor.js) 创建实例

var itemAddEditor;
//页面初始化完毕后执行此方法
$(function () {
var TT = TTO = {
// 编辑器参数
kingEditorParams: {
//指定上传文件参数名称
filePostName: "uploadFile",
//指定上传文件请求的url。
uploadJson: '/pic/upload',
//上传类型,分别为image、flash、media、file
dir: "image"
},
createEditor: function (select) {
return KindEditor.create(select, TT.kingEditorParams);
}
};
//创建富文本编辑器
itemAddEditor = TTO.createEditor("#itemAddForm");
});

4、编写后台 配置虚拟路径(MyWebAppConfiguration)

package com.editors.kindeditor.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration
public class MyWebAppConfiguration extends WebMvcConfigurerAdapter { /**
* 添加一些虚拟路径的映射
* 静态资源路径和上传文件的路径
*
* @param registry
*/
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
/**
* @Description: 对文件的路径进行配置, 创建一个虚拟路径/Path/** ,即只要在< img src="/Path/picName.jpg" />便可以直接引用图片
*这是图片的物理路径 "file:/+本地图片的地址"
*/
registry.addResourceHandler("/Path/**").addResourceLocations("file:/C:/Users/lenovo/AppData/Local/Temp/tomcat-docbase.2975979620477460781.8080/upload/");
super.addResourceHandlers(registry);
} }

5、图片上传服务

package com.editors.kindeditor.controller;

import com.editors.utils.JsonUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.HashMap;
import java.util.Map; @Controller
public class KindeditorController { @RequestMapping("/kindeditor")
public String kindeditor() {
return "kindeditor/kindeditor";
} /**
* 返回 复杂类型 容易产生浏览器兼容性问题
* 原因:
* 跟 @ResponseBody 默认行为有关
* String类型可直接打回页面,而复杂类型无法直接打回,需要先转换为json
*/
@RequestMapping("/pic/upload")
@ResponseBody
public String picUpload(MultipartFile uploadFile, HttpServletRequest request) {
Map map = new HashMap<>();
if (!uploadFile.isEmpty()) {
String saveFileName = uploadFile.getOriginalFilename();
File saveFile = new File(request.getSession().getServletContext().getRealPath("/upload/") + saveFileName);
if (!saveFile.getParentFile().exists()) {
saveFile.getParentFile().mkdirs();
}
String path = "/Path/" + saveFileName;
System.out.println("path={" + path + "}");
try {
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(saveFile));
out.write(uploadFile.getBytes());
out.flush();
out.close();
map.put("error", 0);
map.put("url", path);
} catch (Exception e) {
e.printStackTrace();
map.put("error", 1);
map.put("url", path);
return "上传失败," + e.getMessage();
}
} else {
map.put("error", 1);
map.put("url", "上传失败,因为文件为空.");
}
return JsonUtils.objectToJson(map);
} }

6、执行效果:

coding++:快速构建 kindeditor 富文本编辑器(一)的更多相关文章

  1. django的admin或者应用中使用KindEditor富文本编辑器

    由于django后台管理没有富文本编辑器,看着好丑,展示出来的页面不美观,无法做到所见即所得的编辑方式,所以我们需要引入第三方富文本编辑器. 之前找了好多文档已经博客才把这个功能做出来,有些博客虽然写 ...

  2. KindEditor富文本编辑器使用

    我的博客本来打算使用layui的富文本编辑器,但是出了一个问题,无法获取编辑器内容,我参考官方文档,获取内容也就那几个方法而已,但是引入进去后始终获取的值为空,百度和bing都试过了,但是始终还是获取 ...

  3. kindeditor富文本编辑器初步使用教程

    下载kindeditor 可以选择去官网下载(http://kindeditor.net/down.php),不过要FQ:或者直接CSDNhttp://download.csdn.net/downlo ...

  4. (转)淘淘商城系列——KindEditor富文本编辑器的使用

    http://blog.csdn.net/yerenyuan_pku/article/details/72809794 通过上文的学习,我们知道了怎样解决KindEditor富文本编辑器上传图片时的浏 ...

  5. (转)学习淘淘商城第二十二课(KindEditor富文本编辑器的使用)

    http://blog.csdn.net/u012453843/article/details/70184155 上节课我们一起学习了怎样解决KindEditor富文本编辑器上传图片的浏览器兼容性问题 ...

  6. django项目中使用KindEditor富文本编辑器。

    先从官网下载插件,放在static文件下 前端引入 <script type="text/javascript" src="/static/back/kindedi ...

  7. django项目中使用KindEditor富文本编辑器

    先从官网下载插件,放在static文件下 前端引入 <script type="text/javascript" src="/static/back/kindedi ...

  8. springboot中使用kindeditor富文本编辑器实现博客功能

    kindeditor在之前已经用过,现在在springboot项目中使用.并且也在里面使用了图片上传以及回显等功能. 其实主要的功能是图片的处理:kindeditor对输入的内容会作为html标签处理 ...

  9. kindEditor 富文本编辑器 使用介绍

    第一版:存放位置:  ---->把该创建的文件包放到javaWeb 过程的 WEB_INF 下:如图所示. 第二步:< kindEditor 插件的引用> :JS引用 <scr ...

随机推荐

  1. 逐行分析jQuery2.0.3源码-完整笔记

    概览 (function (){ (21 , 94) 定义了一些变量和函数 jQuery=function(); (96 , 293) 给jQuery对象添加一些方法和属性; (285 , 347) ...

  2. 我折腾的shell笔记

    目录 Mac一些常用的快捷键记录 iTerm2或者命令行相关 Mac桌面上或者某目录下操作 一些实用脚本示例 代码无提示或者其他抽风症状,清除Xcode缓存 查看当前网络ip地址 日常提交推送git代 ...

  3. 最简单易懂的实现CRC16校验

    public String getCRC16(byte[] bytes) { //CRC寄存器全为1 int CRC = 0x0000ffff; //多项式校验值 int POLYNOMIAL = 0 ...

  4. 内网渗透之跨边界传输 - LCX转发

    跨边界转发 端口转发 lcx 流程 目标机 ./lcx -slave 跳板机ip 监听的端口 127.0.0.1 要转发的端口 跳板机(公网) ./lcx -listen 监听的端口 转发给攻击机访问 ...

  5. vue的watch

    watch它可以用来监测Vue实例上的数据变动  尽量一张图解释清楚(尝试用圈圈区分关系): 写的很简单,watch本来就没啥东西我理解为响应式侦听全局变量 watch里绑定全局变量,被绑定全局变量发 ...

  6. django setting文件那些事

    1.设置语言.时区 2.设置新建的用户表作为默认用户表 3.利用apps文件夹收纳app 新建python package apps,然后把app放在该文件夹下 然后setting中添加如下代码: 4 ...

  7. 《ASP.NET Core 3框架揭秘》博文汇总

    在过去一段时间内,写了一系列关于ASP.NET Core 3相关的文章,其中绝大部分来源于即将出版的<ASP.NET Core 3框架揭秘>(博文只能算是"初稿",与书 ...

  8. C++ 判断两个圆是否有交集

    #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include <math.h> #include <easyx.h ...

  9. js 实现简易留言板功能

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  10. WEB应用之http协议和httpd闲聊

    什么是web?在日常生活中我们常常听到web这个词,它到底是什么呢?今天我们来聊一聊web应用http协议:相信生活在如今互联网时代的我们,http这个协议应该对我们不是很陌生吧!比如双十一双十二我们 ...