SpringBoot 集成 CKEditor4 完整实例:
快速开始:
首先我们需要新建一个SpringBoot的项目,将CKEditor4集成进来。
-
下载CKEditor 标准版即可
-
将其解压放在SpringBoot项目的static目录下:
-
引入官方提供的例子: (修改正确的js地址)
edit.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>A Simple Page with CKEditor</title>
<!-- Make sure the path to CKEditor is correct. -->
<script src="ckeditor/ckeditor.js"></script>
</head>
<body>
<form>
<textarea name="editor1" id="editor1" rows="10" cols="80">
This is my textarea to be replaced with CKEditor.
</textarea>
<script>
// Replace the <textarea id="editor1"> with a CKEditor
// instance, using default configuration.
CKEDITOR.replace( 'editor1' );
</script>
</form>
</body>
</html>
此时目录结构如下:
随后启动项目,可以在浏览器中看到效果:
上传图片:
此时的CKEditor是无法将我们本地的图片放上去的,需要我们做一些配置:
在解压后的ckeditor中修改config.js 文件:
config.uploadUrl = '/images/ckeditorUpload?command=QuickUpload&type=Files&responseType=json';
config.filebrowserUploadUrl = '/images/ckeditorUpload?command=QuickUpload&type=Files';
config.filebrowserImageUploadUrl = '/images/ckeditorUpload?command=QuickUpload&type=Images';
后面配置的url 需要我们在controller 中自己处理图片上传的处理:
补充:
由于我们一般都是将SpringBoot项目打成jar包的,所以需要将文件放在本地服务器的其他地方,需要对此做一些修改,applicaion.yml
spring:
servlet:
multipart:
enabled: true
max-file-size: 30MB
max-request-size: 30MB
resources:
static-locations: /META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,file:E:/test
上面的配置是由于上传时请求信息太长,需要对此设置一下,否则在上传图片时会报错。
The field upload exceeds its maximum permitted size of 1048576 bytes.
下面的resources 配置就是本地静态资源文件的映射了,需要全部写上,否则会被覆盖。这样我们在 E:/test 下的文件可以直接像静态资源那样获取到了。
eg: 该目录下的1.jpg 可以通过http://localhost:8080/1.jpg 获取到
上传文件Controller处理:
在此处我们选择使用FileUils 来处理文件上传,添加依赖:
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
CKEditor 需要我们上传成功之后返回json数据,将其封装成为一个对象:
/**
* Created by wangchen on 2019/10/16 15:18
* Description: 上传图片返回信息 CKEditor需要返回json信息
* uploaded 在上传成功时可以设置为1 失败为 0
* fileName 为文件名
* url 为文件的实际访问路径
*/
@Data
public class ImageUploadVo {
private int uploaded;
private String fileName;
private String url;
}
@RestController
@RequestMapping("/images")
@Slf4j
public class ImageUploadController {
private final String imageFilePath = "E:/test/";
/**
* 上传图片
*
* @param file
* @return
*/
@ResponseBody
@RequestMapping("/ckeditorUpload")
public ImageUploadVo ckeditorUpload(@RequestParam("upload") MultipartFile file) throws Exception {
log.info("开始上传图片");
// 获取文件名
String fileName = file.getOriginalFilename();
// 获取文件的后缀名
String suffixName = fileName.substring(fileName.lastIndexOf("."));
String newFileName = UUID.randomUUID().toString() + suffixName;
log.info("上传文件文件名称:{}",newFileName);
log.info("上传文件大小 :{}" + file.getSize());
FileUtils.copyInputStreamToFile(file.getInputStream(), new File(imageFilePath + newFileName));
ImageUploadVo imageUploadVo = new ImageUploadVo();
imageUploadVo.setUploaded(1);
imageUploadVo.setFileName(newFileName);
imageUploadVo.setUrl("http://localhost:8080/" + newFileName);
return imageUploadVo;
}
}
编辑edit.html 添加点击按钮 查看效果:
<div align="center">
<input type="button" onclick="buttonSubmit()" style="width: 60px;height: 35px;" value="提交表单"/>
</div>
<script>
function buttonSubmit() {
var a = CKEDITOR.instances.editor1.getData();
console.log(a);
}
</script>
重新启动项目,直接复制一张图片,可以看到图片可以上传成功,回显也没有问题。
增强图像插件:
在上面中,我们已经实现了图片的上传以及回显,但是上传的图片无法拖动来改变位置。
CKEditor 4.3中引入 的可选增强图像插件支持将图像插入编辑器内容。与CKEditor发行版中默认提供的经典Image插件相比,它提供以下功能:
下面就试试这个插件:
-
首先需要下载增强型图像插件:增强型插件
-
解压之后,放到static/ckeditor/plugins下面
-
编辑 config.js 添加配置:
config.extraPlugins = 'image2,uploadimage';
重新启动项目,查看效果:
可以看到 是 现在我们可以简单的调整大小,并为图像添加标题 。
源码地址:
https://github.com/handsome-owner/ckeditor-test-all.git