image.png

1,入门demo

1,前端代码

  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <title>KindEditor JSP</title>
  6. <link rel="stylesheet" href="static/kindeditor/themes/default/default.css" />
  7. <link rel="stylesheet" href="static/kindeditor/plugins/code/prettify.css" />
  8. <script charset="utf-8" src="static/kindeditor/kindeditor.js"></script>
  9. <script charset="utf-8" src="static/kindeditor/lang/zh_CN.js"></script>
  10. <script charset="utf-8" src="static/kindeditor/plugins/code/prettify.js"></script>
  11. <script>
  12. // 富文本框加载完成
  13. KindEditor.ready(function(K) {
  14. // 此处使用创建textarea的name属性
  15. var editor1 = K.create('textarea[name="content1"]', {
  16. cssPath : 'static/kindeditor/plugins/code/prettify.css',
  17. // 图片上传的url
  18. uploadJson : 'http://localhost:8080/uploadJson',
  19. // 查看以往上传图片的位置
  20. fileManagerJson : 'http://localhost:8080/fileManagerJson',
  21. allowFileManager : true,
  22. afterCreate : function() {
  23. var self = this;
  24. K.ctrl(document, 13, function() {
  25. self.sync();
  26. document.forms['example'].submit();
  27. });
  28. K.ctrl(self.edit.doc, 13, function() {
  29. self.sync();
  30. document.forms['example'].submit();
  31. });
  32. }
  33. });
  34. prettyPrint();
  35. //如果需要进行数据回显,在此处将数据设置进富文本编辑框
  36. });
  37. </script>
  38. </head>
  39. <body>
  40. <form name="example" method="post" action="#">
  41. // 富文本位置,
  42. <textarea name="content1" cols="100" rows="8" style="width:700px;height:200px;visibility:hidden;"></textarea>
  43. <br />
  44. <input type="submit" name="button" value="提交内容" /> (提交快捷键: Ctrl + Enter)
  45. </form>
  46. </body>
  47. </html>

image.png

2,后台controller

  1. package com.moonhu.springboot.upload;
  2. import com.alibaba.fastjson.JSONObject;
  3. import org.apache.commons.fileupload.FileItemFactory;
  4. import org.apache.commons.fileupload.disk.DiskFileItemFactory;
  5. import org.apache.commons.fileupload.servlet.ServletFileUpload;
  6. import org.springframework.util.FileCopyUtils;
  7. import org.springframework.web.bind.annotation.RequestMapping;
  8. import org.springframework.web.bind.annotation.RequestMethod;
  9. import org.springframework.web.bind.annotation.RestController;
  10. import org.springframework.web.multipart.MultipartFile;
  11. import org.springframework.web.multipart.MultipartHttpServletRequest;
  12. import javax.servlet.http.HttpServletRequest;
  13. import javax.servlet.http.HttpServletResponse;
  14. import java.io.File;
  15. import java.io.IOException;
  16. import java.io.PrintWriter;
  17. import java.text.SimpleDateFormat;
  18. import java.util.*;
  19. @RestController
  20. public class UtilsController {
  21. // 文件保存位置
  22. private String uploadPrefix = "D:\\";
  23. private String getError(String message) {
  24. JSONObject obj = new JSONObject();
  25. obj.put("error", 1);
  26. obj.put("message", message);
  27. return obj.toJSONString();
  28. }
  29. /**
  30. 图片上传,单张图片上传成功,多长图片上传有待验证
  31. */
  32. @RequestMapping(value = "/uploadJson", method = {RequestMethod.POST, RequestMethod.GET})
  33. public void uploadJson(HttpServletRequest request, HttpServletResponse response, String dir) throws Exception {
  34. response.setContentType("application/json; charset=UTF-8");
  35. PrintWriter out = response.getWriter();
  36. MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
  37. Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
  38. Set<String> keySet = fileMap.keySet();
  39. for (String s : keySet) {
  40. // 获取multipart
  41. MultipartFile multipartFile = fileMap.get(s);
  42. // 执行存储动作
  43. // url返回结果封装
  44. JSONObject obj = new JSONObject();
  45. obj.put("error", 0);
  46. obj.put("url", "此处填写图片的全路径");
  47. out.println(obj.toJSONString());
  48. }
  49. /**
  50. 图片管理(图片上传面板上的图片空间请求地址)
  51. */
  52. @RequestMapping("/fileManagerJson")
  53. public void fileManagerJson(HttpServletRequest request, HttpServletResponse response) throws Exception {
  54. response.setContentType("application/json; charset=UTF-8");
  55. PrintWriter out = response.getWriter();
  56. //根目录路径,可以指定绝对路径,比如 /var/www/attached/
  57. String rootPath = uploadPrefix + "/attached/";
  58. //根目录URL,可以指定绝对路径,比如 http://www.yoursite.com/attached/
  59. String rootUrl = request.getContextPath() + "/attached/";
  60. //图片扩展名
  61. String[] fileTypes = new String[]{"gif", "jpg", "jpeg", "png", "bmp"};
  62. String dirName = request.getParameter("dir");
  63. if (dirName != null) {
  64. if (!Arrays.<String>asList(new String[]{"image", "flash", "media", "file"}).contains(dirName)) {
  65. out.println("Invalid Directory name.");
  66. return;
  67. }
  68. rootPath += dirName + "/";
  69. rootUrl += dirName + "/";
  70. File saveDirFile = new File(rootPath);
  71. if (!saveDirFile.exists()) {
  72. saveDirFile.mkdirs();
  73. }
  74. }
  75. //根据path参数,设置各路径和URL
  76. String path = request.getParameter("path") != null ? request.getParameter("path") : "";
  77. String currentPath = rootPath + path;
  78. String currentUrl = rootUrl + path;
  79. String currentDirPath = path;
  80. String moveupDirPath = "";
  81. if (!"".equals(path)) {
  82. String str = currentDirPath.substring(0, currentDirPath.length() - 1);
  83. moveupDirPath = str.lastIndexOf("/") >= 0 ? str.substring(0, str.lastIndexOf("/") + 1) : "";
  84. }
  85. //排序形式,name or size or type
  86. String order = request.getParameter("order") != null ? request.getParameter("order").toLowerCase() : "name";
  87. //不允许使用..移动到上一级目录
  88. if (path.indexOf("..") >= 0) {
  89. out.println("Access is not allowed.");
  90. return;
  91. }
  92. //最后一个字符不是/
  93. if (!"".equals(path) && !path.endsWith("/")) {
  94. out.println("Parameter is not valid.");
  95. return;
  96. }
  97. //目录不存在或不是目录
  98. File currentPathFile = new File(currentPath);
  99. if (!currentPathFile.isDirectory()) {
  100. out.println("Directory does not exist.");
  101. return;
  102. }
  103. //遍历目录取的文件信息
  104. List<Hashtable> fileList = new ArrayList<Hashtable>();
  105. if (currentPathFile.listFiles() != null) {
  106. for (File file : currentPathFile.listFiles()) {
  107. Hashtable<String, Object> hash = new Hashtable<String, Object>();
  108. String fileName = file.getName();
  109. if (file.isDirectory()) {
  110. hash.put("is_dir", true);
  111. hash.put("has_file", (file.listFiles() != null));
  112. hash.put("filesize", 0L);
  113. hash.put("is_photo", false);
  114. hash.put("filetype", "");
  115. } else if (file.isFile()) {
  116. String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
  117. hash.put("is_dir", false);
  118. hash.put("has_file", false);
  119. hash.put("filesize", file.length());
  120. hash.put("is_photo", Arrays.<String>asList(fileTypes).contains(fileExt));
  121. hash.put("filetype", fileExt);
  122. }
  123. hash.put("filename", fileName);
  124. hash.put("datetime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(file.lastModified()));
  125. fileList.add(hash);
  126. }
  127. }
  128. if ("size".equals(order)) {
  129. Collections.sort(fileList, new SizeComparator());
  130. } else if ("type".equals(order)) {
  131. Collections.sort(fileList, new TypeComparator());
  132. } else {
  133. Collections.sort(fileList, new NameComparator());
  134. }
  135. JSONObject result = new JSONObject();
  136. result.put("moveup_dir_path", moveupDirPath);
  137. result.put("current_dir_path", currentDirPath);
  138. result.put("current_url", currentUrl);
  139. result.put("total_count", fileList.size());
  140. result.put("file_list", fileList);
  141. out.println(result.toJSONString());
  142. }
  143. }

2,面板配置

1,面板上item设置

image.png

1.找到kindeditor.js中的items属性.

image.png

2.修改面板

image.png
每一项代表一个功能,把不需要的内容删除即可
注意:如果需要按钮在指定位置换行,在指定的位置添加一个 **“/“** 即可

2,修改面板初始化大小

在前端页面创建富文本对象的时候进行大小的控制
image.png

3,修改视频的embed标签

1.将video添加为不需要过滤过的标签

  1. video : ['id', 'class', 'src', 'width', 'height',
  2. 'type', 'loop', 'autoplay', 'quality', '.width',
  3. '.height', 'align', 'allowscriptaccess','controls'],

kindeditor-4.1.10 - 图7

2.加上MP4视频类型

  1. if (/\.(mp4)(\?|$)/i.test(src)) {
  2. return 'video/mp4';
  3. }

kindeditor-4.1.10 - 图8

3.根据视频类型判断

  1. if(attrs.type=="video/mp4"){
  2. var html = '<video ';
  3. _each(attrs, function(key, val) {
  4. html += key + '="' + val + '" ';
  5. });
  6. html += 'controls="controls"/>';
  7. }

kindeditor-4.1.10 - 图9

最终效果如图:
kindeditor-4.1.10 - 图10

4,将文件的回显地址设置成全路径

1.找到kindeditor插件kindeditor-all.js,找到urltype的设置成”absolute”

image.png

2.全局搜索 “absolute”,将如下代码注释即可

image.png