`
ljjclub
  • 浏览: 35034 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

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

阅读更多
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;
	}
}
1
0
分享到:
评论

相关推荐

    java定时从ftp服务器更新相关文件

    Java定时从FTP服务器更新相关文件是一项常见的任务,特别是在自动化数据同步和备份的场景中。这里主要涉及的技术点包括FTP协议、Java编程以及文件系统操作。本文将深入探讨这些知识点,并提供一个基于`ftp4j`库的...

    FTP远程文件同步更新

    3. 智能同步:通过比较文件的修改时间或内容哈希值来确定是否需要更新。 FTP的安全性: 尽管FTP方便,但其传输数据时默认不加密,可能导致敏感信息暴露。因此,现在更推荐使用FTPS(FTP over SSL/TLS)或SFTP(SSH ...

    pdf.js在java web项目中远程预览ftp上的pdf文件.docx

    "pdf.js在java web项目中远程预览ftp上的pdf文件" 标题:pdf.js在java web项目中远程预览ftp上的pdf文件 描述:本文档详细介绍了使用pdf.js在Java web项目中远程预览FTP服务器上的PDF文件的方法。通过配置pdf.js...

    FTP远程文件同步更新程序 v2.0.0.0

    FTP远程文件同步更新程序v2.0.0.0是一款专为用户设计的高效文件同步工具,它通过FTP(File Transfer Protocol)协议帮助用户实现本地计算机与远程服务器之间的文件自动或手动更新。这款软件适用于网站管理员、开发者...

    java实现的远程ftp文件浏览

    FtpList部分是用来显示FTP服务器上的文件; GetButton部分为从FTP服务器下传一个文件; PutButton部分为向FTP服务器上传一个文件。 别忘了在程序中还要引入两个库文件(import sun.net.*,import sun.net.ftp.*)。 以下...

    Java 实现对比ftp文件和本地文件的修改时间 更新本地文件

    Java 实现对比ftp文件和本地文件的修改时间 ftp服务器文件比本地文件新时 把服务器文件下载覆盖本地文件 更新本地文件

    FTP远程文件同步更新程序

    FTP远程文件同步更新程序 版本:2.1.0.0 新版本v2.1.0.0改动 1.解决了2G以上文件传输时的bug,能够正确比对传输大型文件。 2.支持了程序启动后自动开始同步操作的功能。 3.修正了前版本中的多处bug,进一步...

    FTP远程上传下载文件

    FTP(File Transfer Protocol)是一种广泛使用的网络协议,用于在互联网上进行文件传输。在这个场景中,我们使用FTP协议来实现在远程服务器上上传和下载文件的功能。C#和Asp.NET是开发此类应用的主要编程语言和技术...

    FTP下载自动更新文件

    1. FTP协议:FTP是互联网上用于文件传输的标准协议,它允许用户从一个远程服务器上下载或上传文件。FTP使用客户端-服务器模型,客户端发送请求,服务器响应并执行操作。 2. 多线程下载:为了提高下载速度和效率,...

    ftp 上传远程文件

    在这个过程中,“ftp 上传远程文件”指的是通过FTP服务将本地计算机上的文件发送到远程服务器的过程。 FTP的工作原理基于TCP/IP协议栈,它分为两种模式:主动模式和被动模式。主动模式中,客户端打开一个端口(通常...

    Delphi根据文件更新时间上传下载的FTP程序..rar

    本项目“Delphi根据文件更新时间上传下载的FTP程序”是一个利用Delphi构建的FTP客户端应用,它具有检查本地文件与远程服务器上文件的修改时间的功能,并能自动上传或下载更新的文件。以下将详细解析这个项目的知识点...

    springboot以FTP方式上传文件到远程服务器

    "Spring Boot 使用 FTP 方式上传文件到远程服务器" 在本文中,我们将详细介绍如何使用 Spring Boot 框架来实现 FTP 方式上传文件到远程服务器。FTP(File Transfer Protocol)是一种常用的文件传输协议,广泛应用于...

    asp.net C# 利用FTP 远程下载文件

    FTP(File Transfer Protocol)则是一种标准网络协议,用于在Internet上进行文件传输。本教程将详细讲解如何使用C#在ASP.NET环境中实现FTP远程下载文件到指定的本地盘符。 首先,要进行FTP远程下载,你需要引入`...

    springboot以FTP方式上传文件到远程服务器的流程

    Spring Boot 中使用 FTP 上传文件到远程服务器的流程 在本文中,我们将介绍如何使用 Spring Boot 实现 FTP 上传文件到远程服务器的流程。这个流程包括如何使用 JWT 登录认证及鉴权的流程,以及如何使用 Spring ...

    用FTP 远程下载文件

    在本场景中,我们讨论的是如何使用PowerBuilder(PB)6.5版本通过FTP程序来实现远程文件下载,进而自动化PB程序的更新过程。下面我们将深入探讨相关知识点。 首先,PowerBuilder(PB)是一款强大的Windows应用程序...

    FTP.rar_PowerBuilder 服务_ftp_ftp 对比时间_ftp 时间 文件_对比更新

    PowerBuilder 服务_ftp_ftp 对比时间_ftp 时间 文件_对比更新”揭示了这个压缩包文件主要涉及的是使用PowerBuilder开发的一个FTP(File Transfer Protocol)客户端应用,该应用具备文件时间对比和自动更新功能。...

    PHP远程上传文件到FTP

    在PHP编程中,远程上传文件到FTP服务器是一项常见的任务,特别是在网站内容管理、文件分发或备份场景下。本文将详细讲解如何使用PHP实现这一功能,以及涉及的相关知识点。 首先,要实现远程上传,我们需要了解PHP的...

    ftp文件定时传输,适合远程文件备份

    在这个场景下,用户提到的需求是实现一个自定义程序,该程序能够自动检测本地文件的变化,并在有新文件或文件更新时通过FTP协议将它们上传到远程备份服务器,确保文件的完整性和一致性。下面将详细阐述这一过程涉及...

    ftp 远程dos命令上传文件

    FTP(File Transfer Protocol)是一种广泛使用的网络协议,用于在互联网上进行文件传输。在这个场景中,我们使用DOS命令行下的FTP客户端来实现文件的远程上传。DOS,即Disk Operating System,是早期Windows系统中的...

Global site tag (gtag.js) - Google Analytics