此案例 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. CMSampleBufferRef解析

    CMTime:64位的value,32位的scale, media的时间格式 CMVideoFormatDesc:video的格式,包括宽高.颜色空间.编码格式.SPS.PPS CVPixelBuff ...

  2. nx-admin1.2版本发布

    nx-admin 是一个开源的管理系统前端集成方案 github地址 nx-admin的初心 组件更易用, 让每个小白快速上手, 最大程度上帮助个人,企业节省时间成本和费用开支. 尽管这个过程不简单, ...

  3. sqlserver取分组数据的最后一条数据

    SQL Server中ROW_NUMBER()函数的使用 参考文章:https://blog.csdn.net/pan_junbiao/article/details/79941162 业务中的问题: ...

  4. node中fs模块 - fs.open() fs.read() fs.write() fs.close()

    var fs = require('fs') fs.open('./a.txt', 'a+', function(err, fd) { // 打开文件后 创建缓冲区放置数据 ), // 读取多少字节 ...

  5. 题解-[HNOI2001]遥控赛车比赛

    题解-[HNOI2001]遥控赛车比赛 前置知识:记忆化搜索.\(\texttt{Bfs}\). 参考资料 https://www.luogu.com.cn/blog/CYJian/solution- ...

  6. Android模拟器不能上网的解决方法

    我原来一直不用Android的模拟器,因为这东西的多年前的印象真的是很糟糕——启动半个小时,不支持OpenGL.即使后来有了x86镜像,在HAXM的支持下快的飞起,也不想用,因为NDK还要编译x86的 ...

  7. Redis面试题集锦(精选)

    1.什么是 Redis?简述它的优缺点? Redis的全称是:Remote Dictionary.Server,本质上是一个Key-Value 类型的内存数据库,很像memcached,整个数据库统统 ...

  8. CyclicBarrier源码探究 (JDK 1.8)

    CyclicBarrier也叫回环栅栏,能够实现让一组线程运行到栅栏处并阻塞,等到所有线程都到达栅栏时再一起执行的功能."回环"意味着CyclicBarrier可以多次重复使用,相 ...

  9. 外部配置属性值是如何被绑定到XxxProperties类属性上的?--SpringBoot源码(五)

    注:该源码分析对应SpringBoot版本为2.1.0.RELEASE 1 前言 本篇接 SpringBoot是如何实现自动配置的?--SpringBoot源码(四) 温故而知新,我们来简单回顾一下上 ...

  10. LoadRunner初入门(安装)

    在经过了两天网上找软件-真机上装软件-完了发现真机不能用(不能用的原因就是IE不能打开 试了很多方法现在真机上的ie变成了ie8英文版),果断用上了虚拟机 虚拟机刚开始要装镜像 一开始下的是64位的发 ...