spring boot集成ckeditor以及上传图片
将上面的下载好的demo放入所需的项目工程中去
在需要引入富文本编辑器的地方加上这段代码
Ckeditor编辑器上传图片
/**
-
ckeditor图片上传
-
@Title imageUpload
-
@param request
-
@param response
*/
@RequestMapping(value="/admin/imageUpload", method=RequestMethod.POST)
public void imageUpload(HttpServletRequest request, HttpServletResponse response) {
String DirectoryName = “upload/article”;
String relativePath = env.getProperty(“image.file.article.dir”);
System.out.println(“relativePath=========”+relativePath);
try {
ImageUploadUtil.ckeditor(request, response, DirectoryName,relativePath);
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
Controller 层spring MVC
/**
-
ckeditor文件上传功能,回调,传回图片路径,实现预览效果。
-
@Title ckeditor
-
@param request
-
@param response
-
@param DirectoryName
-
文件上传目录:比如upload(无需带前面的/) upload/…
-
@throws IOException
*/
public static void ckeditor(HttpServletRequest request, HttpServletResponse response, String DirectoryName,String relativePath)
throws IOException {
String fileName = upload(request, DirectoryName,relativePath);
// 结合ckeditor功能
// imageContextPath为图片在服务器地址,如upload/123.jpg,非绝对路径
String imageContextPath = request.getContextPath() + “/” + DirectoryName + “/” + fileName;
response.setContentType(“text/html;charset=UTF-8”);
String callback = request.getParameter(“CKEditorFuncNum”);
PrintWriter out = response.getWriter();
out.println("<script type=“text/javascript”>");
out.println(“window.parent.CKEDITOR.tools.callFunction(” + callback + “,’” + imageContextPath + “’,’’” + “)”);
out.println("");
out.flush();
out.close();
}
/**
-
图片上传
-
@Title upload
-
@param request
-
@param DirectoryName
-
文件上传目录:比如upload(无需带前面的/) upload/news …
-
@return
-
@throws IllegalStateException
-
@throws IOException
*/
public static String upload(HttpServletRequest request, String DirectoryName,String relativePath) throws IllegalStateException,
IOException {
// 创建一个通用的多部分解析器
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession()
.getServletContext());
// 图片名称
String fileName = null;
// 判断 request 是否有文件上传,即多部分请求
if (multipartResolver.isMultipart(request)) {
// 转换成多部分request
MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
// 取得request中的所有文件名
Iterator iter = multiRequest.getFileNames();
while (iter.hasNext()) {
// 记录上传过程起始时的时间,用来计算上传时间
// int pre = (int) System.currentTimeMillis();
// 取得上传文件
MultipartFile file = multiRequest.getFile(iter.next());
if (file != null) {
// 取得当前上传文件的文件名称
String myFileName = file.getOriginalFilename();
// 如果名称不为“”,说明该文件存在,否则说明该文件不存在
if (myFileName.trim() != “”) {
// 获得图片的原始名称
String originalFilename = file.getOriginalFilename();
// 获得图片后缀名称,如果后缀不为图片格式,则不上传
String suffix = originalFilename.substring(originalFilename.lastIndexOf(".")).toLowerCase();
if (!fileTypes.contains(suffix)) {
continue;
}
// 当前app根目录
String rootPath = Thread.currentThread().getContextClassLoader().getResource("").getPath();
// 文件夹是否存在,不存在就创建
File dir = new File(rootPath + File.separator + relativePath);
if (!dir.exists())
dir.mkdirs();
// 重命名上传后的文件名 111112323.jpg
fileName = RandomUUID.getUuid() + suffix;
// 定义上传路径 …/upload/111112323.jpg
File uploadFile = new File(dir + “\” + fileName);
System.out.println(uploadFile);
file.transferTo(uploadFile);
System.out.println(fileName);
}
}
// 记录上传该文件后的时间
// int finaltime = (int) System.currentTimeMillis();
// System.out.println(finaltime - pre);
}
}
return fileName;
}