zoukankan      html  css  js  c++  java
  • 【ckeditor系列】ckeditor 图片上传最新完美处理

    ckeditor的不可以上传图片,ckfinder又要收费,当然如果你是用的php,那么你可以用kcfinder,免费的。

    那怎么解决呢?

    ckeditor 图片上传最新完美处理

    需要做的有以下几步: 
    1. 到官网下载ckeditor,网址:http://ckeditor.com/download 
    2. 复制到java web项目目录下 
    3. 配置config文件,打开图片上传功能 
       

    Javascript代码 
    1. CKEDITOR.editorConfig = function (config) {  
      // 换行方式  
          config.enterMode = CKEDITOR.ENTER_BR;  
        
          // 当输入:shift+Enter是插入的标签  
          config.shiftEnterMode = CKEDITOR.ENTER_BR;//   
          //图片处理  
          config.pasteFromWordRemoveStyles = true;  
          config.filebrowserImageUploadUrl = "ckUploadImage.action?type=image";  
            
          // 去掉ckeditor“保存”按钮  
          config.removePlugins = 'save';  
      };  

    ** 在这里另附flex引用ckeditor 编辑器默认全屏方法: 
    修改ckeditor.html页面: 

    Javascript代码 
    1. <script type="text/javascript">  
              if ( typeof CKEDITOR == 'undefined' )  
              {  
                document.write('加载失败') ;  
              }  
              else  
              {  
                //var editor = CKEDITOR.replace( 'editor1',  
                //     {  
                  //      customConfig: "news_ck_config.js"  
              //      });  
                 
                var editor = CKEDITOR.replace( 'editor1');  
                        
                CKEDITOR.on('instanceReady', function(evt){  
                   var editor = evt.editor;  
                   editor.execCommand('maximize');  
                });  
              }  
        
      </script>  



    4. java后台处理代码 

    Javascript代码 
    1. // 上传图片  
          @Action(value = "/ckUploadImage", results = { @Result(name = "success", location = "/upload.jsp") })  
          public String uploadImages() throws Exception {  
              HttpServletRequest request = ServletActionContext.getRequest();  
              FileOutputStream fos;  
              String webRoot = request.getSession().getServletContext().getRealPath(  
                      "");  
              // 获取图片后缀名  
              String partRightType = uploadFileName.substring(uploadFileName  
                      .lastIndexOf("."));  
              String CKEditorFuncNum = request.getParameter("CKEditorFuncNum");  
              // 判断图片的格式  
              if (!ImageFile.checkImageType(partRightType)) {  
                  String path = "";  
                  String alt_msg = "Sorry! Image format selection is incorrect, please choose GIF, jpeg, PNG format JPG, picture!";  
                  pringWriterToPage("<script type='text/javascript'>window.parent.CKEDITOR.tools.callFunction("  
                          + CKEditorFuncNum  
                          + ", '"  
                          + path  
                          + "' , '"  
                          + alt_msg  
                          + "');</script>");  
              } else {  
                  try {  
        
                      uploadFileName = DateUtils.getDateNoStyle() + "-"  
                              + UUID.randomUUID() + partRightType;  
        
                      String savePath = webRoot + Constants.UPLOAD_IMAGES_PATH;  
                      File uploadFilePath = new File(savePath);  
                      if (uploadFilePath.exists() == false) {  
                          uploadFilePath.mkdirs();  
                          System.out.println("路径不存在,但是已经成功创建了" + savePath);  
                      } else {  
                          System.out.println("路径存在了" + savePath);  
                      }  
                      fos = new FileOutputStream(new File(savePath + uploadFileName));  
                      FileInputStream fis = new FileInputStream(getUpload());  
                      byte[] buffer = new byte[1024];  
                      int len = 0;  
                      while ((len = fis.read(buffer)) > 0) {  
                          fos.write(buffer, 0, len);  
                      }  
                      fos.close();  
                      fis.close();  
                  } catch (FileNotFoundException foe) {  
                      System.out.println("上传文件为0字节");  
                  }  
        
                  // String path = "http://" + request.getServerName() + ":"  
                  // + request.getServerPort() + request.getContextPath()  
                  // + Constants.UPLOAD_IMAGES_PATH + uploadFileName;  
        
                  String path = request.getContextPath()  
                          + Constants.UPLOAD_IMAGES_PATH + uploadFileName;  
                  String alt_msg = "";  
                  pringWriterToPage("<script type='text/javascript'>window.parent.CKEDITOR.tools.callFunction("  
                          + CKEditorFuncNum  
                          + ", '"  
                          + path  
                          + "' , '"  
                          + alt_msg  
                          + "');</script>");  
        
              }  
        
              return null;  
          }  



    * 其实重点的代码就是这点,相信大家都能看的懂,有不懂的留言! 
    pringWriterToPage("<script type='text/javascript'>window.parent.CKEDITOR.tools.callFunction(" 
    + CKEditorFuncNum 
    + ", '" 
    + path 
    + "' , '" 
    + alt_msg 
    + "');</script>");

  • 相关阅读:
    3.Java入门程序,在dos下运行Hello World
    2.javase环境的搭建
    1.Java语言的简介
    常见的dos命令
    计算机基础
    网络工程师-个人笔记三
    网络工程个人笔记二
    网络工程师个人笔记一
    Vuex
    cookie session sessionStorage localStorage
  • 原文地址:https://www.cnblogs.com/101rico/p/2867535.html
Copyright © 2011-2022 走看看