<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

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

    需要使用jcifs-1.3.16.jar
    官網(wǎng)下載地址如下:
    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;
        
        
    /**
         * 由本地復(fù)制文件至遠(yuǎn)程共享目錄
         * 
    @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();
            }
     
        }


        
    /**
         * 由遠(yuǎn)程復(fù)制文件至本地
         * 
    @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>();
            
    //遠(yuǎn)程機(jī)器IP地址
            ls.add(0"192.168.60.90");
            
    //遠(yuǎn)程機(jī)器用戶名
            ls.add(1"Administrator");
            
    //遠(yuǎn)程機(jī)器密碼
            ls.add(2"password");
            
    //遠(yuǎn)程機(jī)器共享目錄名字
            ls.add(3"/sameE/");
            
    // 本地共享目錄絕對路徑
            ls.add(4"E:/sameE/");

            
    // 拼接連接遠(yuǎn)程機(jī)器共享目錄的串
            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(
    "本地覆蓋遠(yuǎn)程");
                    }

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

                }

                
    else{
                    
    /**
                     * 遠(yuǎn)程不存在,從本地復(fù)制到遠(yuǎn)程機(jī)器
                     
    */

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

            }

            
    for(String key_lan : map_lan.keySet()){
                File file_local 
    = map_local.get(key_lan);
                
    // 本地不存在,從遠(yuǎn)程復(fù)制到本地
                if(file_local==null){
                    copyFromLanToLocal(map_lan.get(key_lan),ls.get(
    4));
                    System.out.println(
    "不存在,從遠(yuǎn)程復(fù)制到本地"+
    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
         
    */

        
    // 該方法實現(xiàn)要讀取文件的過濾
        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();
        }

    }



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


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

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


    網(wǎng)站導(dǎo)航:
     

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

    常用鏈接

    留言簿

    隨筆檔案

    文章分類

    搜索

    •  

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 猫咪免费观看人成网站在线| 国产亚洲sss在线播放| 久久久久亚洲av无码专区| 亚洲性猛交xx乱| 亚洲 欧洲 视频 伦小说| 黄网站色视频免费看无下截| 亚洲精品偷拍视频免费观看| 四虎成人精品永久免费AV| 免费影院未满十八勿进网站| 四虎影视免费永久在线观看| 亚洲性猛交XXXX| 亚洲福利电影一区二区?| 亚洲αⅴ无码乱码在线观看性色| 人碰人碰人成人免费视频| 免费A级毛片无码专区| 成年女人18级毛片毛片免费| 国产乱辈通伦影片在线播放亚洲 | 中文亚洲AV片在线观看不卡| 亚洲AV日韩精品久久久久久 | 亚洲成人午夜电影| 亚洲sm另类一区二区三区| 成全视频免费观看在线看| 久久受www免费人成_看片中文| 亚洲成av人在片观看| 亚洲av不卡一区二区三区| 亚洲精品无码中文久久字幕| 免费在线看黄的网站| 国产精品美女自在线观看免费| 国产亚洲高清不卡在线观看| 亚洲日韩精品国产一区二区三区| 岛国精品一区免费视频在线观看| 国产在线jyzzjyzz免费麻豆| 亚洲国产精品自产在线播放 | 亚洲AV日韩AV永久无码免下载| 亚洲男同gay片| 免费国产成人α片| 免费a级黄色毛片| 亚洲国产精品久久丫| 国产大片免费天天看| 成人免费无码大片a毛片| 亚洲av网址在线观看|