环境描述: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.blogjava.net/Files/xggc63/lanFileSyn.rar
posted on 2011-09-22 16:52
鲍国钰 阅读(657)
评论(0) 编辑 收藏