ckeditor上传图片

ckeditor版本4.4.7

CKEditor的编辑器工具栏中有一项“图片域”,该工具可以贴上图片地址来在文本编辑器中加入图片,但是没有图片上传功能。
技术分享
去掉预览中的英文 ,ckeditor/config.js中加上一个配置项:

config.image_previewText = ‘ ‘; 注意引号里面一定要有个空格,不能是空字符串。


图片上传实现

第一步:

要开启图片上传功能。

找到ckeditor/plugins/image/dialogs/image.js这个文件,CTRL+F搜索下面一段JS:

id:‘Upload‘,hidden:!0    把感叹号去掉

技术分享

这个时候编辑器上已经有上传功能了

技术分享


第二步:(这里用struct2实现上传)

下面是action的代码

  1. package com.sdhf.eam.web.action;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.FileOutputStream;
  5. import java.io.InputStream;
  6. import java.io.OutputStream;
  7. import java.io.PrintWriter;
  8. import javax.servlet.http.HttpServletResponse;
  9. import org.apache.struts2.ServletActionContext;
  10. import com.sdhf.action._BaseAction;
  11. public class CkeditorUploadAction extends _BaseAction{
  12. private File upload;
  13. private String uploadContentType;
  14. private String uploadFileName;
  15. public File getUpload() {
  16. return upload;
  17. }
  18. public void setUpload(File upload) {
  19. this.upload = upload;
  20. }
  21. public String getUploadContentType() {
  22. return uploadContentType;
  23. }
  24. public void setUploadContentType(String uploadContentType) {
  25. this.uploadContentType = uploadContentType;
  26. }
  27. public String getUploadFileName() {
  28. return uploadFileName;
  29. }
  30. public void setUploadFileName(String uploadFileName) {
  31. this.uploadFileName = uploadFileName;
  32. }
  33. public String execute() throws Exception {
  34. HttpServletResponse response = ServletActionContext.getResponse();
  35. response.setCharacterEncoding("GBK");
  36. PrintWriter out = response.getWriter();
  37. // CKEditor提交的很重要的一个参数
  38. String callback = ServletActionContext.getRequest().getParameter("CKEditorFuncNum");
  39. String expandedName = ""; // 文件扩展名
  40. if (uploadContentType.equals("image/pjpeg") || uploadContentType.equals("image/jpeg")) {
  41. // IE6上传jpg图片的headimageContentType是image/pjpeg,而IE9以及火狐上传的jpg图片是image/jpeg
  42. expandedName = ".jpg";
  43. } else if (uploadContentType.equals("image/png") || uploadContentType.equals("image/x-png")) {
  44. // IE6上传的png图片的headimageContentType是"image/x-png"
  45. expandedName = ".png";
  46. } else if (uploadContentType.equals("image/gif")) {
  47. expandedName = ".gif";
  48. } else if (uploadContentType.equals("image/bmp")) {
  49. expandedName = ".bmp";
  50. } else {
  51. out.println("<script type=\"text/javascript\">");
  52. out.println("window.parent.CKEDITOR.tools.callFunction(" + callback
  53. + ",‘‘,‘文件格式不正确(必须为.jpg/.gif/.bmp/.png文件)‘);");
  54. out.println("</script>");
  55. return null;
  56. }
  57. if (upload.length() > 600 * 1024) {
  58. out.println("<script type=\"text/javascript\">");
  59. out.println("window.parent.CKEDITOR.tools.callFunction(" + callback
  60. + ",‘‘," + "‘文件大小不得大于600k‘);");
  61. out.println("</script>");
  62. return null;
  63. }
  64. InputStream is = new FileInputStream(upload);
  65. String uploadPath = ServletActionContext.getServletContext().getRealPath("/images/ckeditor");
  66. String fileName = java.util.UUID.randomUUID().toString(); // 采用UUID的方式命名保证不会重复
  67. fileName += expandedName;
  68. File toFile = new File(uploadPath, fileName);
  69. OutputStream os = new FileOutputStream(toFile);
  70. // 文件复制到指定位置
  71. byte[] buffer = new byte[1024];
  72. int length = 0;
  73. while ((length = is.read(buffer)) > 0) {
  74. os.write(buffer, 0, length);
  75. }
  76. is.close();
  77. os.close();
  78. // 返回“图像”选项卡并显示图片
  79. out.println("<script type=\"text/javascript\">");
  80. out.println("window.parent.CKEDITOR.tools.callFunction(" + callback
  81. + ",‘"+ServletActionContext.getRequest().getContextPath()+"/images/ckeditor/"+ fileName + "‘,‘‘)"); // 相对路径用于显示图片
  82. out.println("</script>");
  83. return null;
  84. }
  85. }
这里有几个注意的地方:
1、 String uploadPath = ServletActionContext.getServletContext().getRealPath("/images/ckeditor");
"/images/ckeditor" 为服务器存放的目录,目录一定要存在,不然会报错
2、技术分享
这个是上传完后前台显示的路径,即下图
技术分享
如果路径没有设置好,预览就会没有图片显示,如图
技术分享

第三步:写xml配置文件,我这里是spring+struct2
applicationContext.xml文件加上
  1. <!-- ckeditor 图片上传action -->
  2. <bean id="ckeditorUpload" class="com.sdhf.eam.web.action.CkeditorUploadAction" parent="_baseAction"
  3. scope="prototype">
  4. </bean>

struts.xml文件加上
  1. <!-- CKEditor 上传图片 -->
  2. <action name="ckeditorUpload" class="ckeditorUpload" method="execute">
  3. </action>

第四步:
在ckeditor/config.js加上配置项
config.filebrowserUploadUrl = ctx+"/ckeditorUpload.action";





郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。