CKEditor图片上传实现详细步骤(使用Struts 2)

本人使用的CKEditor版本是3.6.3。CKEditor配置和部署我就不多说。

CKEditor的编辑器工具栏中有一项“图片域”,该工具可以贴上图片地址来在文本编辑器中加入图片,但是没有图片上传。

“预览”中有一大堆鸟语,看得很不爽。可以打开ckeditor/plugins/image/dialogs/image.js文件,搜索“b.config.image_previewText”就能找到这段鸟语了,(b.config.image_previewText||‘‘)单引号中的内容全删了,注意别删多了。

扫除这个障碍,下面来研究图片上传。

step 1:

首先,还是image.js这个文件,搜索“upload”可以找到这一段

id:‘Upload‘,hidden:true

实际上上传功能被隐藏了,把上面的true改成false,再打开编辑器,就能找到上传功能了。

step 2:

上面的只是一个上传页面。也就相当于一个HTML的form表单,要配置点击“上传到服务器上”按钮后请求的Action。可以在ckeditor/config.js中配置。

加入:

config.filebrowserUploadUrl="actions/ckeditorUpload";

"ckeditorUpload"是请求的URL,也就是点击这个按钮就会post到ckeditorUpload地址进行处理,这里指向的是Struts 2的一个Action。当然,也可以用servlet或者ASP、PHP等来处理请求。

  1. <package name="actions" extends="struts-default" namespace="/actions">
  2. <action name="ckeditorUpload" class="com.xxx.CkeditorUpload ">
  3. </action>
  4. </package>

step 3:

文件上传的控件相当于<input  type="file" name="upload" .../>,其name是”upload”,知道了name那么就可以在Action中获取这个文件。

  1. private File upload;  //文件
  2. private String uploadContentType;  //文件类型
  3. private String uploadFileName;   //文件名

以上三个私有变量都要有set方法。如果不了解的话可以先学习一下Struts 2文件上传。

step 4:

如果上传的图片格式不正确,可以在上传界面进行提示。

这个提示不是ckeditor提示的,要在Action中响应。

  1. String callback = ServletActionContext.getRequest().getParameter("CKEditorFuncNum");
  2. if([判断条件]){
  3. out.println("<script type=\"text/javascript\">");
  4. out.println("window.parent.CKEDITOR.tools.callFunction(" + callback + ",‘‘," + "‘文件格式不正确(必须为.jpg/.gif/.bmp/.png文件)‘);");
  5. out.println("</script>");
  6. return null;
  7. }

step 5:

  1. InputStream is = new FileInputStream(upload);
  2. String uploadPath = ServletActionContext.getServletContext().getRealPath("/img/postImg");
  3. String fileName = java.util.UUID.randomUUID().toString();  //采用UUID的方式随即命名
  4. fileName += expandedName;  // 加上后缀名
  5. File toFile = new File(uploadPath, fileName);
  6. OutputStream os = new FileOutputStream(toFile);
  7. byte[] buffer = new byte[1024];
  8. int length = 0;
  9. while ((length = is.read(buffer)) > 0) {
  10. os.write(buffer, 0, length);
  11. }
  12. is.close();
  13. os.close();

这段代码是Struts 2上传图片的核心代码,把图片上传后保存在项目的某个目录下,并随机重命名。

step 6:

图片上传成功,在目录下也可以看到图片,至此图片上传成功。但是如何将图片发到编辑器中呢?

点“确定”按钮会有以下提示。

到这里,要在Action中返回一段JS脚本。

  1. String callback =ServletActionContext.getRequest().getParameter("CKEditorFuncNum");
  2. out.println("<script type=\"text/javascript\">");
  3. out.println("window.parent.CKEDITOR.tools.callFunction("+ callback + ",‘" +"img/postImg/"+ fileName + "‘,‘‘)");
  4. out.println("</script>");

有了这段代码,图片上传成功后,根据这里的

"img/postImg/" + filename

相对地址,就可以使用这个图片,直接转到“图像”页面。

附:Struts 2 Action代码

  1. package com.xxg.bbs.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.opensymphony.xwork2.ActionSupport;
  11. public class CkeditorUpload extends ActionSupport {
  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 + ",‘‘," + "‘文件格式不正确(必须为.jpg/.gif/.bmp/.png文件)‘);");
  53. out.println("</script>");
  54. return null;
  55. }
  56. if(upload.length() > 600*1024){
  57. out.println("<script type=\"text/javascript\">");
  58. out.println("window.parent.CKEDITOR.tools.callFunction(" + callback + ",‘‘," + "‘文件大小不得大于600k‘);");
  59. out.println("</script>");
  60. return null;
  61. }
  62. InputStream is = new FileInputStream(upload);
  63. String uploadPath = ServletActionContext.getServletContext()
  64. .getRealPath("/img/postImg");
  65. String fileName = java.util.UUID.randomUUID().toString();  //采用时间+UUID的方式随即命名
  66. fileName += expandedName;
  67. File toFile = new File(uploadPath, fileName);
  68. OutputStream os = new FileOutputStream(toFile);
  69. byte[] buffer = new byte[1024];
  70. int length = 0;
  71. while ((length = is.read(buffer)) > 0) {
  72. os.write(buffer, 0, length);
  73. }
  74. is.close();
  75. os.close();
  76. // 返回“图像”选项卡并显示图片
  77. out.println("<script type=\"text/javascript\">");
  78. out.println("window.parent.CKEDITOR.tools.callFunction(" + callback + ",‘" + "img/postImg/" + fileName + "‘,‘‘)");
  79. out.println("</script>");
  80. return null;
  81. }
  82. }
时间: 2024-08-03 03:08:22

CKEditor图片上传实现详细步骤(使用Struts 2)的相关文章

.net core CKEditor 图片上传

最近在玩 asp.net core,不想用UEditor,想使用CKEditor.故需要图片上传功能. 废话不多说,先上效果图: CKEditor 前端代码: <text id="content" name="content"></text> <script> CKEDITOR.replace('content'); </script> CKeditor config.js 配置代码:需要配置图片上传路径 CKEDIT

springMVC和ckeditor图片上传

1.下载ckeditor后将文件夹放在webroot的js目录下: 2.springmvc配置静态文件访问 在web.xml中添加: <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.css</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>d

ios之AFN上传下载详细步骤(2)

1 五.AFN 2 1.GET\POST 3 1> GET请求 4 // 1.获得请求管理者 5 AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager]; 6 7 // 2.封装请求参数 8 NSMutableDictionary *params = [NSMutableDictionary dictionary]; 9 params[@"username"] = @"12

ckeditor图片上传二三事

最近实验室要用ckeditor,踩了几个小坑记录下. 1.出现iframe跨域问题 response.setHeader("X-Frame-Options", "SAMEORIGIN");或者在websecurityconfig中添加http.headers().frameOptions().disable(); 2.接参一定要注意前台file的name是什么

项目整合ckeditor实现图片上传到远程服务器

最近手头上的一个Java项目需要做一个门户网站,其中有一个模块就是用来发布最新的业界安全动态的模块,因此需要用到后台发布新闻的功能:刚开始的时候在网上搜了一下,大部分都是关于PHP和.NET的,关于Java不多,而且查到的都是说用ckeditor+ckfinder来实现,ckeditor实现文本的编辑,ckfinder实现图片的上传,刚开始我也是准备用ckeditor+ckfinder来实现的,但是后来研究ckfinder的时候不知道如何配置ckfinder的图片上传路径问题,网上可以找到好多例

CKEditor实现图片上传

本人用的CKEditor版本为4.3 CKEditor配置和部署参考CKEditor4.x部署和配置. CKEditor编辑器的工具栏中初始的时候应该是这样子的,没有图片上传按钮 并且预览中有一堆火星文,可以修改相应配置删除它. 第一种方法:打开ckeditor/plugins/image/dialogs/image.js文件,搜索“b.config.image_previewText”,(b.config.image_previewText||'')单引号中的内容全删了,注意别删多了.(由于c

SpringMVC:学习笔记(10)——整合Ckeditor且实现图片上传

搞清楚路径位置 配置CKEDITOR 精简文件 解压之后可以看到ckeditor/lang下面有很多语言的js,如果不需要那么多种语言的,可以只剩下en.js,zh.js,zh-cn.js 图片上传时图像信息中的预览会显示一堆英文信息,会干扰预览.找到ckeditor/plugins/image/dialogs/image.js,搜索"d.config.image_previewText"就能找到这段鸟语了,(d.config.image_previewText||")引号中

CKEditor不借助CKFinder实现图片上传(.net版 ashx实现)

参考博客:http://blog.csdn.net/mydwr/article/details/8669594 本人版本:4.4.6 打开文件:ckeditor/plugins/image/dialogs/image.js 搜索内容:[c.config.image_previewText],并删掉其后引号内的内容. 删除后格式:[c.config.image_previewText||""]. 与原文差异:原内容原文中是[b.config.image_previewText],我这里是

Drupal 7 配置ckeditor和ckfinder编辑器实现图片上传--不用wysisyg

注意: 1.这里的ckeditor编辑器是独立模块,不是那个wysiwyg模块. 2.这里的图片上传仅仅为文章内图片,非字段图片. 1.下载文件(1) http://drupal.org/project/ckeditor drupal的ckeditor模块(2) http://ckeditor.com/download CKeditor源码(3) http://ckfinder.com/download CKfinder(注意,不是免费的) 将ckedit文件夹放置在/sites/all/mod