精品伊人久久大香线蕉,开心久久婷婷综合中文字幕,杏田冲梨,人妻无码aⅴ不卡中文字幕

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
CKEditor圖片上傳實現詳細步驟(使用Struts 2)

url:   http://blog.csdn.net/xiao__gui/article/details/7684505


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

CKEditor的編輯器工具欄中有一項“圖片域”,該工具可以貼上圖片地址來在文本編輯器中加入圖片,但是沒有圖片上傳。

“預覽”中有一大堆鳥語,看得很不爽。可以打開ckeditor/plugins/image/dialogs/image.js文件,搜索“b.config.image_previewText”就能找到這段鳥語了,(b.config.image_previewText||'')單引號中的內容全刪了,注意別刪多了。

掃除這個障礙,下面來研究圖片上傳。

1.首先,還是image.js這個文件,搜索“upload”可以找到這一段

id:'Upload',hidden:true

實際上上傳功能被隱藏了,把上面的true改成false,再打開編輯器,就能找到上傳功能了。

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

加入:

config.filebrowserUploadUrl="actions/ckeditorUpload";

var pathName = window.document.location.pathname;
    //獲取帶"/"的項目名,如:/uimcardprj
    var projectName = pathName.substring(0, pathName.substr(1).indexOf('/') + 1);
    config.filebrowserImageUploadUrl = projectName+'/system/upload.do'; //固定路徑

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

3.文件上傳的控件相當于<input  type="file" name="upload" .../>,nameupload,知道了name那么就可以在Action中獲取這個文件。

private File upload;  //文件

private String uploadContentType;  //文件類型

private String uploadFileName;   //文件名

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

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

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

HttpServletResponse response =ServletActionContext.getResponse();

response.setCharacterEncoding("GBK");

PrintWriter out = response.getWriter();

                                    

if(???){

            out.print("<font color=\"red\"size=\"2\">*文件格式不正確(必須為.jpg/.gif/.bmp/.png文件)</font>");

            return null;

}

5.

  InputStream is = newFileInputStream(upload);

  String uploadPath = ServletActionContext.getServletContext().getRealPath("/img/postImg");   //設置保存目錄

  String fileName =java.util.UUID.randomUUID(); //采用UUID的方式隨機命名

  fileName+= uploadFileName.substring(uploadFileName.length() - 4);

  File toFile = new File(uploadPath, fileName);

  OutputStream os = new FileOutputStream(toFile);  

  byte[] buffer = new byte[1024];  

  int length = 0;

  while ((length = is.read(buffer)) > 0) {  

        os.write(buffer, 0, length);  

  }  

  is.close();

  os.close();

這段代碼是Struts 2上傳圖片的核心代碼,把圖片上傳后保存在項目的某個目錄下,并隨機重命名。

6.圖片上傳成功,在目錄下也可以看到圖片,至此圖片上傳成功。但是如何將圖片發到編輯器中呢?

點“確定”按鈕會有以下提示。

到這里,要在Action中加入一段JS

String callback =ServletActionContext.getRequest().getParameter("CKEditorFuncNum"); 

out.println("<scripttype=\"text/javascript\">");

out.println("window.parent.CKEDITOR.tools.callFunction("+ callback + ",'" +"img/postImg/"+ fileName + "','')"); 

out.println("</script>");

有了這段代碼,圖片上傳成功后,根據這里的

"img/postImg/" + filename

相對地址,就可以使用這個圖片,直接轉到“圖像”頁面。

附:Struts 2 Action代碼

  1. public class CkeditorUpload extends ActionSupport {  
  2.   
  3.     private File upload;  
  4.     private String uploadContentType;  
  5.     private String uploadFileName;  
  6.   
  7.   
  8.     public File getUpload() {  
  9.         return upload;  
  10.     }  
  11.   
  12.     public void setUpload(File upload) {  
  13.           
  14.         this.upload = upload;  
  15.     }  
  16.   
  17.     public String getUploadContentType() {  
  18.         return uploadContentType;  
  19.     }  
  20.   
  21.     public void setUploadContentType(String uploadContentType) {  
  22.         this.uploadContentType = uploadContentType;  
  23.     }  
  24.   
  25.     public String getUploadFileName() {  
  26.         return uploadFileName;  
  27.     }  
  28.   
  29.     public void setUploadFileName(String uploadFileName) {  
  30.         this.uploadFileName = uploadFileName;   }  
  31.   
  32.     public String execute() throws Exception {  
  33.         HttpServletResponse response = ServletActionContext.getResponse();  
  34.         response.setCharacterEncoding("GBK");  
  35.         PrintWriter out = response.getWriter();  
  36.   
  37.   
  38.         //對文件進行校驗  
  39.         if(upload==null || uploadContentType==null || uploadFileName==null){  
  40.             out.print("<font color=\"red\" size=\"2\">*請選擇上傳文件</font>");  
  41.             return null;  
  42.         }  
  43.           
  44.         if ((uploadContentType.equals("image/pjpeg") || uploadContentType.equals("image/jpeg"))  
  45.                 && uploadFileName.substring(uploadFileName.length() - 4).toLowerCase().equals(".jpg")) {  
  46.             //IE6上傳jpg圖片的headimageContentType是image/pjpeg,而IE9以及火狐上傳的jpg圖片是image/jpeg  
  47.         }else if(uploadContentType.equals("image/png") && uploadFileName.substring(uploadFileName.length() - 4).toLowerCase().equals(".png")){  
  48.               
  49.         }else if(uploadContentType.equals("image/gif") && uploadFileName.substring(uploadFileName.length() - 4).toLowerCase().equals(".gif")){  
  50.               
  51.         }else if(uploadContentType.equals("image/bmp") && uploadFileName.substring(uploadFileName.length() - 4).toLowerCase().equals(".bmp")){  
  52.               
  53.         }else{  
  54.             out.print("<font color=\"red\" size=\"2\">*文件格式不正確(必須為.jpg/.gif/.bmp/.png文件)</font>");  
  55.             return null;  
  56.         }  
  57.           
  58.         if(upload.length() > 600*1024){  
  59.             out.print("<font color=\"red\" size=\"2\">*文件大小不得大于600k</font>");  
  60.             return null;  
  61.         }  
  62.           
  63.         //將文件保存到項目目錄下  
  64.         InputStream is = new FileInputStream(upload);  
  65.         String uploadPath = ServletActionContext.getServletContext()     
  66.                 .getRealPath("/img/postImg");   //設置保存目錄  
  67.         String fileName = java.util.UUID.randomUUID();  //采用UUID的方式隨機命名  
  68.         fileName += uploadFileName.substring(uploadFileName.length() - 4);  
  69.         File toFile = new File(uploadPath, fileName);  
  70.         OutputStream os = new FileOutputStream(toFile);     
  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.           
  80.         //設置返回“圖像”選項卡  
  81.         String callback = ServletActionContext.getRequest().getParameter("CKEditorFuncNum");    
  82.         out.println("<script type=\"text/javascript\">");    
  83.         out.println("window.parent.CKEDITOR.tools.callFunction(" + callback + ",'" + "img/postImg/" + fileName + "','')");    
  84.         out.println("</script>");  
  85.   
  86.           
  87.         return null;  
  88.     }  
  89. }  
作者:叉叉哥   轉載請注明出處:http://blog.csdn.net/xiao__gui/article/details/7684505

本站僅提供存儲服務,所有內容均由用戶發布,如發現有害或侵權內容,請點擊舉報
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
CKEditor實現圖片上傳
讓CKEDITOR支持JSP上傳
CKEDITOR CKFINDER的圖片上傳配置(C#/asp.net/php)
CKEditor上傳圖片配置PHP語言
ExtJS之上傳文件示例【struts2方式】
主題:基于kindeditor3.4的上傳功能的JAVA實現(好)
更多類似文章 >>
生活服務
分享 收藏 導長圖 關注 下載文章
綁定賬號成功
后續可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點擊這里聯系客服!

聯系客服

主站蜘蛛池模板: 万州区| 互助| 香港 | 胶南市| 清流县| 大理市| 平顺县| 蓝山县| 浙江省| 闽清县| 仁化县| 射阳县| 十堰市| 陆河县| 石阡县| 阿拉尔市| 太仆寺旗| 建平县| 启东市| 平和县| 赣州市| 通城县| 扬州市| 呼和浩特市| 阳原县| 隆尧县| 航空| 山东| 习水县| 依兰县| 包头市| 容城县| 喀什市| 文昌市| 郯城县| 磐安县| 霸州市| 西和县| 南雄市| 朝阳县| 昆明市|