欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

从远程ftp上按修改时间或文件名称更新相关文件(原创)

程序员文章站 2022-05-11 22:13:53
...
package com.bj.ftp;

/**
 *需要ftp4j.jar
 *author http://faithlee.iteye.com
 */

import it.sauronsoftware.ftp4j.FTPAbortedException;
import it.sauronsoftware.ftp4j.FTPClient;
import it.sauronsoftware.ftp4j.FTPDataTransferException;
import it.sauronsoftware.ftp4j.FTPException;
import it.sauronsoftware.ftp4j.FTPFile;
import it.sauronsoftware.ftp4j.FTPIllegalReplyException;
import it.sauronsoftware.ftp4j.FTPListParseException;

import java.io.File;
import java.io.IOException;
http://faithlee.iteye.com
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import com.bj.util.CommonStringDateUtil;
import com.bj.util.PropertiesUtil;

public class FtpTool {
	
	private FTPClient client = new FTPClient();
	
	/**
	 * 连接且登录到特定的FTP服务器文件夹
	 * 
	 * @return
	 * @throws FTPException
	 * @throws FTPIllegalReplyException
	 * @throws IOException
	 * @throws IllegalStateException
	 */
	public boolean connectToServer(PropertiesUtil pro,String filename) throws IllegalStateException, IOException,
			FTPIllegalReplyException, FTPException {
		
		String server = pro.getValue(filename, "ftp.ip");
		String user = pro.getValue(filename, "ftp.user");
		String password = pro.getValue(filename, "ftp.password");
		String path = pro.getValue(filename, "ftp.path");

		client.connect(server);
		client.login(user, password);
		System.out.println("登陆成功");

		client.changeDirectory(path);

		if (client.isCompressionSupported()) {
			client.setCompressionEnabled(true);
		}
		return true;
	}

	/**http://faithlee.iteye.com
	 * // 根据修改时间更新相同文件名的文件 
	 * public boolean doUpdate() throws
	 * IllegalStateException, IOException, FTPIllegalReplyException,
	 * FTPException, FTPDataTransferException, FTPAbortedException,
	 * FTPListParseException { Boolean bl = null; 
	 * Map<String,FileObject> remotefiles = getRemotefiles(); 
	 * Map<String,FileObject> localfiles = getLocalfiles(); 
	 * // 按远程文件信息对比本地文件信息 
	 * Iterator it= remotefiles.keySet().iterator(); 
	 * while (it.hasNext()) 
	 * { String key =(String) it.next(); 
	 * FileObject localFile = localfiles.get(key);
	 * FileObject remoteFile = remotefiles.get(key); 
	 * if (localFile == null &&remoteFile != null) 
	 * { doDownload(remotefiles); bl = true;} 
	 * //根据本地和远程文件的最后修改时间判断下不下载。 
	 * Date localFileTime =CommonStringDateUtil.parse(localFile.lasttime); 
	 * Date remoteFileTime =CommonStringDateUtil .parse(remoteFile.lasttime); 
	 * if(localFileTime.before(remoteFileTime)) { 
	 * delete();doDownload(remotefiles); bl = true; } 
	 * else { bl = false; }
	 * } return bl; }
	 */
	
	/**
	 根据文件名变化更新文件
	 */
	public boolean doUpdate(String localdir) throws IllegalStateException, IOException,
			FTPIllegalReplyException, FTPException, FTPDataTransferException,
			FTPAbortedException, FTPListParseException {
		Boolean bl = false;
		// 取远程文件信息
		Map<String, FileObject> remotefiles = getRemotefiles();
		// 取本地文件信息
		Map<String, FileObject> localfiles = getLocalfiles(localdir);
		// 按远程文件信息对比本地文件信息

		if (localfiles.size() == 0 && remotefiles.size() != 0) {
			System.out.println("本地为空");
			bl = true;
			System.out.println("开始下载远程文件");
			doDownload(remotefiles,localdir);
			System.out.println("下载完成");
		} else {
			Iterator<String> it = remotefiles.keySet().iterator();
			while (it.hasNext()) {
				String key = (String) it.next();

				if (!localfiles.containsKey(key)) {

					System.out.println("需要更新");
					System.gc();
					delete(localdir);
					System.out.println("删除完成");
					System.out.println("开始下载新文件");
					doDownload(remotefiles,localdir);
					System.out.println("下载完成");
					bl = true;
					break;
				} else {
					bl = false;
				}
			}
		}
		return bl;
	}

	/**
	 * 取服务器文件
	 * 
	 * @return
	 * @throws FTPListParseException
	 * @throws FTPAbortedException
	 * @throws FTPDataTransferException
	 * @throws FTPException
	 * @throws FTPIllegalReplyException
	 * @throws IOException
	 * @throws IllegalStateException
	 */

	private  Map<String, FileObject> getRemotefiles()
			throws IllegalStateException, IOException,
			FTPIllegalReplyException, FTPException, FTPDataTransferException,
			FTPAbortedException, FTPListParseException {
		FTPFile[] files = client.list();
		Map<String, FileObject> rtn = new HashMap<String, FileObject>();
		for (FTPFile file : files) {
			if (".".equals(file.getName()) || "..".equals(file.getName())) {
				continue;
			}
			FileObject o = new FileObject();
			o.filename = file.getName();
			o.lasttime = CommonStringDateUtil.format(file.getModifiedDate());

			rtn.put(o.filename, o);
		}
		return rtn;
	}

	/**
	 * 取本地文件夹的所有文件信息
	 * 
	 * @return
	 */
	private  Map<String, FileObject> getLocalfiles(String localdir) {
		File f = new File(localdir);
		if (!f.exists()) {
			System.out.println("目录不存在,创建目录");
			f.mkdirs();
		}
		Map<String, FileObject> rtn = new HashMap<String, FileObject>();
		if (f.isDirectory()) {
			for (File file : f.listFiles()) {
				FileObject fo = new FileObject();
				fo.filename = file.getName();
				fo.lasttime = CommonStringDateUtil.format(new Date(file
						.lastModified()));
				rtn.put(fo.filename, fo);
			}
		}

		return rtn;
	}

	/**
	 * 删除本地文件
	 * 
	 */
	private void delete(String localdir) {
		File f = new File(localdir);
		File delFile[] = f.listFiles();
		int i = f.listFiles().length;
		
		for (int j = 0; j < i; j++) {
			delFile[j].delete();
		}
	}

	/**
	 * 下载ftp服务器文件
	 */
	private  void doDownload(Map<String, FileObject> remotefiles ,String localdir)
			throws IllegalStateException, IOException,
			FTPIllegalReplyException, FTPException, FTPDataTransferException,
			FTPAbortedException {

		for (String filename : remotefiles.keySet()) {

			client.download(filename, new File(localdir + filename));

		}
	}

	public void close() throws IllegalStateException, IOException,
			FTPIllegalReplyException, FTPException {

		client.disconnect(true);

	}

}

class FileObject {
	String filename;
	String lasttime;

	@Override
	public String toString() {
		return filename + "|" + lasttime;
	}
}