<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
    環境描述: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.tkk7.com/Files/xggc63/lanFileSyn.rar


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

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


    網站導航:
     

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

    常用鏈接

    留言簿

    隨筆檔案

    文章分類

    搜索

    •  

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 亚洲免费电影网站| 久久亚洲国产精品123区| 亚洲AV色欲色欲WWW| 亚洲男女内射在线播放| 日韩电影免费观看| 亚洲熟女www一区二区三区| 亚洲一级特黄大片无码毛片 | 精品免费tv久久久久久久| 亚洲人成激情在线播放| 亚洲国产精品自在拍在线播放| 色猫咪免费人成网站在线观看| 久久精品国产亚洲av天美18| 亚洲AV综合色一区二区三区| 免费高清在线影片一区| 国产麻豆一精品一AV一免费| 亚洲乱妇老熟女爽到高潮的片| 日韩va亚洲va欧洲va国产| 免费羞羞视频网站| 久久不见久久见免费视频7| 黄页网站在线免费观看| 亚洲精品视频观看| 亚洲一区精品无码| 麻豆国产人免费人成免费视频| 午夜精品射精入后重之免费观看| 美女裸免费观看网站| 亚洲性色成人av天堂| 亚洲伊人成无码综合网 | 国产成人精品日本亚洲专区| 希望影院高清免费观看视频| AAAAA级少妇高潮大片免费看| 亚洲欧洲日产国码久在线| 亚洲好看的理论片电影| 亚洲综合亚洲综合网成人| 精品国产一区二区三区免费看| 伊人久久免费视频| 91免费在线视频| 日韩免费码中文在线观看| 亚洲日本在线电影| 久久精品亚洲AV久久久无码| 亚洲av永久无码精品网站| 亚洲一区二区三区无码影院|