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;
}
}
分享到:
相关推荐
• 更改自定义命令快捷键不能设置修改标志, 并且更改无法保存 • 查看或编辑一个文件时, 状态栏队列剩余数显示不正确 • 重命名文件夹时, 它的子文件夹缓存没有清除 • 在某种情况下, 由于一个错误而导致列表命令...
基于给定的文件信息,以下是对JUNIPer SRX配置相关知识点的详细解析: ### 操作模式与配置模式 JUNIPer SRX设备提供了多种操作模式来满足不同的配置需求,包括配置模式、查看模式以及帮助模式。配置模式允许用户...
• 改进了从 Adobe Reader 或其它程序中复制 ftp:// 类型的 URL 时对剪贴板的监视处理 • 改进了当处理服务器脱机错误消息时的 PRET 支持 (drftpd) 修正 • 某种情况下, 用户初次断开与站点的连接会触发自动重新...
设置缓存开关:用户可自己选择开启或关闭缓存功能,以及过期时间,自动删除过期的缓存! 后台配置分类:可自定义导航以及左栏分类,填写分类名称和对应分类ID即可! 蜘蛛屏蔽功能:可以选择禁用一些蜘蛛。 IP屏蔽...
V5.40.001更新内容: 后台功能移植到本地,解除远程后台控制! 整合淘宝JSSDK,采用新API申请流程。 所有模板设均增加了B2C推广的页面,后台设置导航等地方,可以选择类型为搜B2C商城。需要申请59秒接口。 修改了文章...
本版无任何限制,无远程域名控制,后台功能完美本地内置,全部可用 本版本首次加入了淘宝以外的接口。具体可在后台配置中心,导航管理里面,导航类别选择B2C商品,就能在该栏目调用B2C商品。包括京东,亚马逊等推广商城...