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:

上面的只是一個上傳頁面。也就相當於一個HTMLform表單,要配置點擊“上傳到服務器上”按鈕后請求的Action。可以在ckeditor/config.js中配置。

加入:

config.filebrowserUploadUrl="actions/ckeditorUpload";

"ckeditorUpload"是請求的URL,也就是點擊這個按鈕就會post到ckeditorUpload地址進行處理,這里指向的是Struts 2的一個Action。當然,也可以用servlet或者ASPPHP等來處理請求。

[html]  view plain copy
  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" .../>,其nameupload,知道了name那么就可以在Action中獲取這個文件。

[java]  view plain copy
  1. private File upload;  //文件  
  2. private String uploadContentType;  //文件類型  
  3. private String uploadFileName;   //文件名  

以上三個私有變量都要有set方法。如果不了解的話可以先學習一下Struts 2文件上傳。

step 4:

如果上傳的圖片格式不正確,可以在上傳界面進行提示。


這個提示不是ckeditor提示的,要在Action中響應。

[java]  view plain copy
  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:

[java]  view plain copy
  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腳本

[java]  view plain copy
  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代碼

[java]  view plain copy
  1. package com.xxg.bbs.action;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileInputStream;  
  5. import java.io.FileOutputStream;  
  6. import java.io.InputStream;  
  7. import java.io.OutputStream;  
  8. import java.io.PrintWriter;  
  9.   
  10. import javax.servlet.http.HttpServletResponse;  
  11.   
  12. import org.apache.struts2.ServletActionContext;  
  13.   
  14. import com.opensymphony.xwork2.ActionSupport;  
  15.   
  16. public class CkeditorUpload extends ActionSupport {  
  17.   
  18.     private File upload;  
  19.     private String uploadContentType;  
  20.     private String uploadFileName;  
  21.   
  22.   
  23.     public File getUpload() {  
  24.         return upload;  
  25.     }  
  26.   
  27.     public void setUpload(File upload) {  
  28.           
  29.         this.upload = upload;  
  30.     }  
  31.   
  32.     public String getUploadContentType() {  
  33.         return uploadContentType;  
  34.     }  
  35.   
  36.     public void setUploadContentType(String uploadContentType) {  
  37.         this.uploadContentType = uploadContentType;  
  38.     }  
  39.   
  40.     public String getUploadFileName() {  
  41.         return uploadFileName;  
  42.     }  
  43.   
  44.     public void setUploadFileName(String uploadFileName) {  
  45.         this.uploadFileName = uploadFileName;  
  46.     }  
  47.   
  48.     public String execute() throws Exception {  
  49.   
  50.         HttpServletResponse response = ServletActionContext.getResponse();  
  51.         response.setCharacterEncoding("GBK");  
  52.         PrintWriter out = response.getWriter();  
  53.   
  54.         // CKEditor提交的很重要的一個參數  
  55.         String callback = ServletActionContext.getRequest().getParameter("CKEditorFuncNum");   
  56.           
  57.         String expandedName = "";  //文件擴展名  
  58.         if (uploadContentType.equals("image/pjpeg") || uploadContentType.equals("image/jpeg")) {  
  59.             //IE6上傳jpg圖片的headimageContentType是image/pjpeg,而IE9以及火狐上傳的jpg圖片是image/jpeg  
  60.             expandedName = ".jpg";  
  61.         }else if(uploadContentType.equals("image/png") || uploadContentType.equals("image/x-png")){  
  62.             //IE6上傳的png圖片的headimageContentType是"image/x-png"  
  63.             expandedName = ".png";  
  64.         }else if(uploadContentType.equals("image/gif")){  
  65.             expandedName = ".gif";  
  66.         }else if(uploadContentType.equals("image/bmp")){  
  67.             expandedName = ".bmp";  
  68.         }else{  
  69.             out.println("<script type=\"text/javascript\">");    
  70.             out.println("window.parent.CKEDITOR.tools.callFunction(" + callback + ",''," + "'文件格式不正確(必須為.jpg/.gif/.bmp/.png文件)');");   
  71.             out.println("</script>");  
  72.             return null;  
  73.         }  
  74.           
  75.         if(upload.length() > 600*1024){  
  76.             out.println("<script type=\"text/javascript\">");    
  77.             out.println("window.parent.CKEDITOR.tools.callFunction(" + callback + ",''," + "'文件大小不得大於600k');");   
  78.             out.println("</script>");  
  79.             return null;  
  80.         }  
  81.           
  82.           
  83.         InputStream is = new FileInputStream(upload);  
  84.         String uploadPath = ServletActionContext.getServletContext()     
  85.                 .getRealPath("/img/postImg");  
  86.         String fileName = java.util.UUID.randomUUID().toString();  //采用時間+UUID的方式隨即命名  
  87.         fileName += expandedName;  
  88.         File toFile = new File(uploadPath, fileName);  
  89.         OutputStream os = new FileOutputStream(toFile);     
  90.         byte[] buffer = new byte[1024];     
  91.         int length = 0;  
  92.         while ((length = is.read(buffer)) > 0) {     
  93.             os.write(buffer, 0, length);     
  94.         }     
  95.         is.close();  
  96.         os.close();  
  97.           
  98.         // 返回“圖像”選項卡並顯示圖片  
  99.         out.println("<script type=\"text/javascript\">");    
  100.         out.println("window.parent.CKEDITOR.tools.callFunction(" + callback + ",'" + "img/postImg/" + fileName + "','')");    
  101.         out.println("</script>");  
  102.           
  103.         return null;  
  104.     }  
  105. }  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM