環(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) 編輯 收藏