3. 對(duì) UEditor 文件上傳的源碼的理解

這里以上傳圖片為例

  1. 先決條件

    • 你已經(jīng)看完了上一篇 : ueditor 之 holleworld宗挥,因?yàn)槲沂鞘褂蒙弦黄拈_發(fā)環(huán)境
    • 你已經(jīng)知道如何搭建 ueditor 的 helloworld
  2. 問題

    1. ueditor 上傳圖片的過程不受我們控制,編輯過程產(chǎn)生的垃圾(例如:用戶上傳后沒有使用的圖片)不知如何清理雀彼?
    2. 上傳圖片只能保存在本服務(wù)器上(繼承上一篇的開發(fā)環(huán)境:也就本 tomcat 上)廓译,使得我們無法為本項(xiàng)目配置單獨(dú)一臺(tái)圖片服務(wù)器進(jìn)行開發(fā)、生產(chǎn)(服務(wù)器集群)
  3. 尋找后端入口 :

    (com.baidu.ueditor.ActionEnter)

    查看 ueditor.config.js

  4. 下載 ueditor 源碼

    ueditor官網(wǎng)下載鏈接

  5. 刪除 ueditor jar包

  6. 引入源碼

    幾個(gè)比較重要的類(突然然發(fā)現(xiàn)這些名字都起的好好,見名知義):

    1. ActionEnter.java
      后臺(tái)入口捌归,調(diào)用 CongigManager 加載 jsp/config.json 的配置信息,根據(jù)不同請(qǐng)求執(zhí)行不同的邏輯代碼

      1. 構(gòu)造方法岭粤,初始化的時(shí)候 利用 CongigManager 加載 jsp/config.json 的數(shù)據(jù)
              public ActionEnter ( HttpServletRequest request, String rootPath ) {
              this.request = request;
              this.rootPath = rootPath;
              this.actionType = request.getParameter( "action" );
              this.contextPath = request.getContextPath();
              this.configManager = ConfigManager.getInstance( this.rootPath, this.contextPath, request.getRequestURI() );
              
          }
      
      1. 根據(jù)不同請(qǐng)求執(zhí)行不同的邏輯代碼
              public String invoke() {
          
          if ( actionType == null || !ActionMap.mapping.containsKey( actionType ) ) {
              return new BaseState( false, AppInfo.INVALID_ACTION ).toJSONString();
          }
          
          if ( this.configManager == null || !this.configManager.valid() ) {
              return new BaseState( false, AppInfo.CONFIG_ERROR ).toJSONString();
          }
          
          State state = null;
          
          int actionCode = ActionMap.getType( this.actionType );
          
          Map<String, Object> conf = null;
          
          switch ( actionCode ) {
          
              case ActionMap.CONFIG:
                  return this.configManager.getAllConfig().toString();
                  
              case ActionMap.UPLOAD_IMAGE:
              case ActionMap.UPLOAD_SCRAWL:
              case ActionMap.UPLOAD_VIDEO:
              case ActionMap.UPLOAD_FILE:
                  conf = this.configManager.getConfig( actionCode );
                  state = new Uploader( request, conf ).doExec();
                  break;
                  
              case ActionMap.CATCH_IMAGE:
                  conf = configManager.getConfig( actionCode );
                  String[] list = this.request.getParameterValues( (String)conf.get( "fieldName" ) );
                  state = new ImageHunter( conf ).capture( list );
                  break;
                  
              case ActionMap.LIST_IMAGE:
              case ActionMap.LIST_FILE:
                  conf = configManager.getConfig( actionCode );
                  int start = this.getStartIndex();
                  state = new FileManager( conf ).listFile( start );
                  break;
                  
          }
          
          return state.toJSONString();
          
      }
      
      
    2. ConfigManager.java

      配置管理器 : 看下面代碼對(duì)比一下 jsp/config.json 就知道了

      
          public Map<String, Object> getConfig ( int type ) {
          
          Map<String, Object> conf = new HashMap<String, Object>();
          String savePath = null;
          
          switch ( type ) {
          
              case ActionMap.UPLOAD_FILE:
                  conf.put( "isBase64", "false" );
                  conf.put( "maxSize", this.jsonConfig.getLong( "fileMaxSize" ) );
                  conf.put( "allowFiles", this.getArray( "fileAllowFiles" ) );
                  conf.put( "fieldName", this.jsonConfig.getString( "fileFieldName" ) );
                  savePath = this.jsonConfig.getString( "filePathFormat" );
                  break;
                  
              case ActionMap.UPLOAD_IMAGE:
                  conf.put( "isBase64", "false" );
                  conf.put( "maxSize", this.jsonConfig.getLong( "imageMaxSize" ) );
                  conf.put( "allowFiles", this.getArray( "imageAllowFiles" ) );
                  conf.put( "fieldName", this.jsonConfig.getString( "imageFieldName" ) );
                  savePath = this.jsonConfig.getString( "imagePathFormat" );
                  break;
                  
              case ActionMap.UPLOAD_VIDEO:
                  conf.put( "maxSize", this.jsonConfig.getLong( "videoMaxSize" ) );
                  conf.put( "allowFiles", this.getArray( "videoAllowFiles" ) );
                  conf.put( "fieldName", this.jsonConfig.getString( "videoFieldName" ) );
                  savePath = this.jsonConfig.getString( "videoPathFormat" );
                  break;
                  
              case ActionMap.UPLOAD_SCRAWL:
                  conf.put( "filename", ConfigManager.SCRAWL_FILE_NAME );
                  conf.put( "maxSize", this.jsonConfig.getLong( "scrawlMaxSize" ) );
                  conf.put( "fieldName", this.jsonConfig.getString( "scrawlFieldName" ) );
                  conf.put( "isBase64", "true" );
                  savePath = this.jsonConfig.getString( "scrawlPathFormat" );
                  break;
                  
              case ActionMap.CATCH_IMAGE:
                  conf.put( "filename", ConfigManager.REMOTE_FILE_NAME );
                  conf.put( "filter", this.getArray( "catcherLocalDomain" ) );
                  conf.put( "maxSize", this.jsonConfig.getLong( "catcherMaxSize" ) );
                  conf.put( "allowFiles", this.getArray( "catcherAllowFiles" ) );
                  conf.put( "fieldName", this.jsonConfig.getString( "catcherFieldName" ) + "[]" );
                  savePath = this.jsonConfig.getString( "catcherPathFormat" );
                  break;
                  
              case ActionMap.LIST_IMAGE:
                  conf.put( "allowFiles", this.getArray( "imageManagerAllowFiles" ) );
                  conf.put( "dir", this.jsonConfig.getString( "imageManagerListPath" ) );
                  conf.put( "count", this.jsonConfig.getInt( "imageManagerListSize" ) );
                  break;
                  
              case ActionMap.LIST_FILE:
                  conf.put( "allowFiles", this.getArray( "fileManagerAllowFiles" ) );
                  conf.put( "dir", this.jsonConfig.getString( "fileManagerListPath" ) );
                  conf.put( "count", this.jsonConfig.getInt( "fileManagerListSize" ) );
                  break;
                  
          }
          
          conf.put( "savePath", savePath );
          conf.put( "rootPath", this.rootPath );
          
          return conf;
          
      }
      
      
    
    3. Uploader.java
    
        執(zhí)行下載
    
    4. State.java
        
        處理狀態(tài)接口 ,前后臺(tái)溝通的接口惜索。
        
    5. BaseState.java
    
        State 的其中一個(gè)實(shí)現(xiàn)類里面包含了一個(gè) Map<String ,String > 實(shí)例,通過 Map 添加前臺(tái)需要的參數(shù)剃浇。
        
        1. 前臺(tái)請(qǐng)求規(guī)范
            ![](http://upload-images.jianshu.io/upload_images/4139030-0ccf38bfba31759b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
        
        2. 后臺(tái)返回規(guī)范(以上傳圖片為例)
           ![](http://upload-images.jianshu.io/upload_images/4139030-666d2791beb13a2d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) 
    
    
  7. 具體后臺(tái)運(yùn)行過程巾兆,自己用 debug 工具,多走幾下就知道了(這里就不演示了)

  8. 運(yùn)行項(xiàng)目

下一篇 : ueditor 實(shí)現(xiàn) 自定義上傳

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末虎囚,一起剝皮案震驚了整個(gè)濱河市角塑,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌淘讥,老刑警劉巖圃伶,帶你破解...
    沈念sama閱讀 217,185評(píng)論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異蒲列,居然都是意外死亡窒朋,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,652評(píng)論 3 393
  • 文/潘曉璐 我一進(jìn)店門嫉嘀,熙熙樓的掌柜王于貴愁眉苦臉地迎上來炼邀,“玉大人,你說我怎么就攤上這事剪侮∈媚” “怎么了洛退?”我有些...
    開封第一講書人閱讀 163,524評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)杰标。 經(jīng)常有香客問我兵怯,道長(zhǎng),這世上最難降的妖魔是什么腔剂? 我笑而不...
    開封第一講書人閱讀 58,339評(píng)論 1 293
  • 正文 為了忘掉前任媒区,我火速辦了婚禮,結(jié)果婚禮上掸犬,老公的妹妹穿的比我還像新娘袜漩。我一直安慰自己,他們只是感情好湾碎,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,387評(píng)論 6 391
  • 文/花漫 我一把揭開白布宙攻。 她就那樣靜靜地躺著,像睡著了一般介褥。 火紅的嫁衣襯著肌膚如雪座掘。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,287評(píng)論 1 301
  • 那天柔滔,我揣著相機(jī)與錄音溢陪,去河邊找鬼。 笑死睛廊,一個(gè)胖子當(dāng)著我的面吹牛形真,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播超全,決...
    沈念sama閱讀 40,130評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼没酣,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了卵迂?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,985評(píng)論 0 275
  • 序言:老撾萬榮一對(duì)情侶失蹤绒净,失蹤者是張志新(化名)和其女友劉穎见咒,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體挂疆,經(jīng)...
    沈念sama閱讀 45,420評(píng)論 1 313
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡改览,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,617評(píng)論 3 334
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了缤言。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片宝当。...
    茶點(diǎn)故事閱讀 39,779評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖胆萧,靈堂內(nèi)的尸體忽然破棺而出庆揩,到底是詐尸還是另有隱情俐东,我是刑警寧澤,帶...
    沈念sama閱讀 35,477評(píng)論 5 345
  • 正文 年R本政府宣布订晌,位于F島的核電站虏辫,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏锈拨。R本人自食惡果不足惜砌庄,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,088評(píng)論 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望奕枢。 院中可真熱鬧娄昆,春花似錦、人聲如沸缝彬。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,716評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)跌造。三九已至杆怕,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間壳贪,已是汗流浹背陵珍。 一陣腳步聲響...
    開封第一講書人閱讀 32,857評(píng)論 1 269
  • 我被黑心中介騙來泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留违施,地道東北人互纯。 一個(gè)月前我還...
    沈念sama閱讀 47,876評(píng)論 2 370
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像磕蒲,于是被迫代替她去往敵國(guó)和親留潦。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,700評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容