CKeditor4 上传图片
CKeditor CKeditor4 图片上传    2019-04-28 07:17:16    1084    0    0

 1、首先引用ckeditor并且初始化,这里示例比较简单,也不加样式,版本为ckeditor4.11.4

  1. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  2. <html lang="zh">
  3. <head>
  4.  <title>CKeditor</title>
  5.  <script src="${pageContext.request.contextPath}/static/ckeditor_4.11.4/ckeditor.js"></script>
  6. </head>
  7. <body>
  8. <textarea name="editor" id="editor"></textarea>
  9. <script>
  10.  CKEDITOR.replace('editor',{
  11.  filebrowserImageUploadUrl :"${pageContext.request.contextPath}/file/uploadImages.do?"
  12.  });
  13. </script>
  14. </body>
  15. </html>​

在初始化CKeditor的时候filebrowserImageUploadUrl :"${pageContext.request.contextPath}/file/uploadImages.do?"这里填写处理图片上传的地方,最后的?加上表示支持直接粘贴自动上传,不加只能从图片中选择图片上传

 

接下来就是后台

  1. package cc.acme_me.website.controller;
  2.  
  3. import cc.acme_me.website.utils.PropertiesUtils;
  4. import cc.acme_me.website.utils.ResponseUtil;
  5. import cc.acme_me.website.utils.StringUtils;
  6. import net.sf.json.JSONObject;
  7. import org.springframework.stereotype.Controller;
  8. import org.springframework.util.FileCopyUtils;
  9. import org.springframework.web.bind.annotation.RequestMapping;
  10. import org.springframework.web.bind.annotation.RequestParam;
  11. import org.springframework.web.multipart.MultipartFile;
  12.  
  13. import javax.servlet.http.HttpServletRequest;
  14. import javax.servlet.http.HttpServletResponse;
  15. import java.io.File;
  16. import java.io.FileOutputStream;
  17. import java.io.IOException;
  18. import java.util.HashMap;
  19. import java.util.Map;
  20.  
  21. @Controller
  22. @RequestMapping("/file")
  23. public class FileUploadController {
  24.  
  25.     @RequestMapping("/uploadImages")
  26.     public String uploadImages(@RequestParam("upload") MultipartFile file, HttpServletResponse response, HttpServletRequest request) throws IOException {
  27.         response.setContentType("text/html;charset=utf-8");
  28.         JSONObject jsonObject = new JSONObject();
  29.         try {
  30.             if (file.isEmpty()) {
  31.                 jsonObject.put("uploaded", 0);
  32.                 Map<String, String> resultMap = new HashMap<>();
  33.                 resultMap.put("message", "失败了");
  34.                 jsonObject.put("error", resultMap);
  35.             } else {
  36.                 String fileName = file.getOriginalFilename();
  37.                 fileName = StringUtils.getUUID() + fileName.substring(fileName.lastIndexOf("."));
  38.                 String path = request.getServletContext().getRealPath("/") + PropertiesUtils.getStringValue("imagePath");
  39.                 FileCopyUtils.copy(file.getInputStream(), new FileOutputStream(new File(path + "/" + fileName)));
  40.                 jsonObject.put("uploaded", 1);
  41.                 jsonObject.put("url", "userImage/"+fileName);
  42.  
  43.             }
  44.         } catch (Exception e) {
  45.             e.printStackTrace();
  46.         }
  47.         ResponseUtil.write(response, jsonObject);
  48.         return null;
  49.     }
  50. }

上传图片主要是注意一下json返回的数据

如果成功,返回的数据格式应该是{"uploaded":"1","url":"图片显示的路径"}

如果失败了,返回数据的格式应该是{"uploaded":"0","error":{"message":"上传失败信息"}}

接下来就可以看到演示效果了

当然,也支持图片粘贴上传

Pre: Ubuntu安装nslookup

Next: BBR

1084
Table of content