鮑國鈺的博客
          謀事在人成事在天
          posts - 5,  comments - 6,  trackbacks - 0
          環境描述:F5負載均衡環境,兩臺win2003,64位操作系統web服務器,由于項目需求要求兩臺服務器某文件夾文件需要同步。
          ①文件增量同步,即兩個文件夾比較,取并集。
          ②同名文件獲取修改時間更新的文件作為母本進行覆蓋同步。

          需要使用jcifs-1.3.16.jar
          官網下載地址如下:
          http://jcifs.samba.org

          參考實例代碼自己寫了個程序。
          package com.bgy.filesyn;

          import java.io.BufferedReader;
          import java.io.BufferedWriter;
          import java.io.File;
          import java.io.FileInputStream;
          import java.io.FileNotFoundException;
          import java.io.FileOutputStream;
          import java.io.IOException;
          import java.io.InputStreamReader;
          import java.io.OutputStreamWriter;
          import java.io.UnsupportedEncodingException;
          import java.util.LinkedHashMap;
          import java.util.LinkedList;
          import java.util.List;
          import java.util.Map;

          import jcifs.smb.SmbException;
          import jcifs.smb.SmbFile;
          import jcifs.smb.SmbFileInputStream;
          import jcifs.smb.SmbFileOutputStream;

          public class LANCopyFile {

              
              
          private static Map<String,SmbFile> map_lan = new LinkedHashMap<String,SmbFile>();
              
              
          private static Map<String,File> map_local = new LinkedHashMap<String,File>();
              
              
          private static long splitNumber =1000*120;
              
              
          /**
               * 由本地復制文件至遠程共享目錄
               * 
          @param filetmp
               * 
          @param lan_path
               
          */

              
          public static void copyFromLocalToLan(File filetmp,String lan_path){
                  
          try {
                      
                      BufferedReader in 
          = new BufferedReader(new InputStreamReader(
                              
          new FileInputStream(filetmp),"UTF-8"));
                      BufferedWriter fos 
          = new BufferedWriter(new OutputStreamWriter(
                              
          new SmbFileOutputStream(
                                      
          new SmbFile(lan_path + filetmp.getName())),"UTF-8"));
                      String str; 
                      
          while ((str = in.readLine()) != null){
                           fos.write(str);
                           System.out.println(str);
                       }

                      in.close();
                      fos.flush();
                      fos.close();
                  }
           catch (UnsupportedEncodingException e) {
                      
          // TODO Auto-generated catch block
                      e.printStackTrace();
                  }
           catch (FileNotFoundException e) {
                      
          // TODO Auto-generated catch block
                      e.printStackTrace();
                  }
          catch (IOException e) {
                      
          // TODO Auto-generated catch block
                      e.printStackTrace();
                  }
           
              }


              
          /**
               * 由遠程復制文件至本地
               * 
          @param filetmp
               * 
          @param local_path
               
          */

              
          public static void copyFromLanToLocal(SmbFile filetmp, String local_path){
                  
                  
          try {
                      
                      BufferedReader in 
          = new BufferedReader(new InputStreamReader(
                              
          new SmbFileInputStream(filetmp),"UTF-8"));
                      BufferedWriter fos 
          = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(
                              
          new File(local_path + filetmp.getName())),"UTF-8"));
                      String str; 
                      
          while ((str = in.readLine()) != null){
                           fos.write(str);
                           System.out.println(str);
                       }

                      in.close();
                      fos.flush();
                      fos.close();
                  }
           catch (UnsupportedEncodingException e) {
                      
          // TODO Auto-generated catch block
                      e.printStackTrace();
                  }
           catch (FileNotFoundException e) {
                      
          // TODO Auto-generated catch block
                      e.printStackTrace();
                  }
          catch (IOException e) {
                      
          // TODO Auto-generated catch block
                      e.printStackTrace();
                  }
           
                  
              }

              
              
          public static void doSyn()throws Exception {
          //         String host = "192.168.60.90";
                  String host = "192.168.60.90";
                  String password 
          = "19861029";
                  String path 
          = "/sameE/";
                  String output_path 
          = "E:/sameE/";
                  List
          <String> ls = new LinkedList<String>();
                  
          //遠程機器IP地址
                  ls.add(0"192.168.60.90");
                  
          //遠程機器用戶名
                  ls.add(1"Administrator");
                  
          //遠程機器密碼
                  ls.add(2"password");
                  
          //遠程機器共享目錄名字
                  ls.add(3"/sameE/");
                  
          // 本地共享目錄絕對路徑
                  ls.add(4"E:/sameE/");

                  
          // 拼接連接遠程機器共享目錄的串
                  String full_path = "smb://" + ls.get(1+ ":" + ls.get(2+ "@"
                          
          + ls.get(0+ ls.get(3+ (ls.get(3).endsWith("/"? "" : "/");

                  
          // 輸出路徑
                  System.out.println("\u6E90\u76EE\u5F55\uFF1A" + full_path);
                  System.out.println(
          "\u76EE\u6807\u76EE\u5F55\uFF1A" + ls.get(4));

                  SmbFile dir 
          = new SmbFile(full_path);
                  
                  
          // SelectFile類用來指定同步文件的類型
                  SmbFile[] files = dir.listFiles(new SelectFile());
                  
                  File files_local 
          = new File(ls.get(4));
                  File[] arr_files 
          = files_local.listFiles();
                  
          for(int i=0;i<arr_files.length;i++){
                      File file 
          = arr_files[i];
                      
          if(file.isDirectory()){
                          
          continue;
                      }

                  }

                  
                  
          for (int i = 0; i < files.length; i++{
                      SmbFile filetmp 
          = files[i];
                  }

                  
                  
          for(int i = 0; i < files.length; i++){
                      
          if(files[i].isDirectory()){
                          
          continue;
                      }

                      map_lan.put(files[i].getName(), files[i]);
                  }

                  
          for(int i = 0; i < arr_files.length; i++){
                      
          if(arr_files[i].isDirectory()){
                          
          continue;
                      }

                      map_local.put(arr_files[i].getName(), arr_files[i]);
                  }

                  
                  
          // 遍歷比對
                  for(String key_local : map_local.keySet()){
                      File file_local 
          = map_local.get(key_local);
                      SmbFile file_lan 
          = map_lan.get(key_local);
                      
          if(file_lan!=null){
                          
          long time_local = file_local.lastModified()/splitNumber;
                          
          long time_lan = file_lan.lastModified()/splitNumber;
                          
                          System.out.println(time_local);
                          System.out.println(time_lan);
                          
          if(time_local==time_lan){
                              System.out.println(
          "無需更改");
                              
          continue;
                          }

                          
          else if(time_local>time_lan){
                              
          // 刪除
                              file_lan.delete();
                              copyFromLocalToLan(file_local,full_path);
                              System.out.println(
          "本地覆蓋遠程");
                          }

                          
          else if(time_local<time_lan){
                              file_local.delete();
                              copyFromLanToLocal(file_lan,ls.get(
          4));
                              System.out.println(
          "遠程覆蓋本地");
                          }

                      }

                      
          else{
                          
          /**
                           * 遠程不存在,從本地復制到遠程機器
                           
          */

                          copyFromLocalToLan(file_local,full_path);
                          System.out.println(
          "不存在,從本地復制到遠程機器"+file_local.getName());
                      }

                  }

                  
          for(String key_lan : map_lan.keySet()){
                      File file_local 
          = map_local.get(key_lan);
                      
          // 本地不存在,從遠程復制到本地
                      if(file_local==null){
                          copyFromLanToLocal(map_lan.get(key_lan),ls.get(
          4));
                          System.out.println(
          "不存在,從遠程復制到本地"+
          package com.bgy.filesyn;

          import jcifs.smb.SmbFile;
          import jcifs.smb.SmbFilenameFilter;

          public class SelectFile implements SmbFilenameFilter {
              
              
          public SelectFile() {
                  
              }


              
          /**
               * * accept * *
               * 
               * 
          @param smbFile
               *            SmbFile *
               * 
          @param string
               *            String *
               * 
          @return boolean
               
          */

              
          // 該方法實現要讀取文件的過濾
              public boolean accept(SmbFile dir, String name) {
                  
          if (name != null && name.endsWith(".xml")) {
                      
          return true;
                  }
           else {
                      
          return false;
                  }

              }

          }
          map_lan.get(key_lan).getName());
                      }

                  }

                  
              }

              
              
              
          public static void main(String[] args)throws Exception {
                  doSyn();
                  Thread.sleep(
          5000);
                  doSyn();
                  Thread.sleep(
          5000);
                  doSyn();
              }

          }



          注:
          ①讀取,寫入文件時,統一使用UTF-8字符集。
          ②此方法進行修改后,可以考慮使用在第三臺機器上,同步另外兩臺機器。
          源文件下載地址http://www.aygfsteel.com/Files/xggc63/lanFileSyn.rar


          posted on 2011-09-22 16:52 鮑國鈺 閱讀(672) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           

          <2011年9月>
          28293031123
          45678910
          11121314151617
          18192021222324
          2526272829301
          2345678

          常用鏈接

          留言簿

          隨筆檔案

          文章分類

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 犍为县| 英山县| 正定县| 磴口县| 山阳县| 永济市| 奈曼旗| 张家港市| 卢湾区| 平阳县| 山东| 富民县| 安阳市| 陵水| 安化县| 罗山县| 循化| 肥东县| 克拉玛依市| 灌云县| 旬阳县| 汾阳市| 贵溪市| 昌宁县| 佛学| 隆化县| 林甸县| 东阿县| 余庆县| 白水县| 佛冈县| 蓝山县| 兰考县| 彭泽县| 江都市| 肥东县| 达尔| 嘉黎县| 连江县| 磐石市| 探索|