使用ftp4j-1.5,见[url]http://www.sauronsoftware.it/projects/ftp4j/index.php[/url]。
界面如下:
[img]http://dl.iteye.com/upload/attachment/254010/5c9967ae-de07-300c-a1f3-1277aa1c7f63.jpg[/img]
1、登录到一个FTP服务器的特定文件夹,取回所有文件信息。
2、检索所有当前文件夹文件信息。
3、遍历所有远程服务器有的文件,若本地有此文件名的文件,则对比其大小。
4、下载所有远程服务器有而本地没有的文件和远程服务器文件大小与本地文件大小不同的文件。
功能简单,但足够使用了。
FTP实现代码:
进度监控代码:
源代码见附件。
界面如下:
[img]http://dl.iteye.com/upload/attachment/254010/5c9967ae-de07-300c-a1f3-1277aa1c7f63.jpg[/img]
1、登录到一个FTP服务器的特定文件夹,取回所有文件信息。
2、检索所有当前文件夹文件信息。
3、遍历所有远程服务器有的文件,若本地有此文件名的文件,则对比其大小。
4、下载所有远程服务器有而本地没有的文件和远程服务器文件大小与本地文件大小不同的文件。
功能简单,但足够使用了。
FTP实现代码:
package cn.stjauns.logan.updater;
import it.sauronsoftware.ftp4j.FTPAbortedException;
import it.sauronsoftware.ftp4j.FTPClient;
import it.sauronsoftware.ftp4j.FTPDataTransferException;
import it.sauronsoftware.ftp4j.FTPDataTransferListener;
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.FileNotFoundException;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import org.apache.log4j.Logger;
import cn.stjauns.logan.tools.CommonDlgUtil;
import cn.stjauns.logan.tools.CommonPropertiesUtil;
import cn.stjauns.logan.tools.CommonStringDateUtil;
public class Updater {
private FTPClient client = new FTPClient();
protected Monitor monitor = new Monitor();
public void doTest() {
logger.info(getLocalfiles());
}
/**
* 更新
*
* @throws FTPException
* @throws FTPIllegalReplyException
* @throws IOException
* @throws IllegalStateException
* @throws FTPListParseException
* @throws FTPAbortedException
* @throws FTPDataTransferException
*/
public boolean doUpdate() throws IllegalStateException, IOException,
FTPIllegalReplyException, FTPException, FTPDataTransferException,
FTPAbortedException, FTPListParseException {
monitor.setVisible(true);
if (!connectToServer()) {
return false;
}
// 取远程文件信息
monitor.setText("取远程文件信息" + "...");
Map<String, FileObject> remotefiles = getRemotefiles();
if (remotefiles.size() == 0) {
CommonDlgUtil.warning("警告", "更新服务器上没有文件!", monitor);
return true;
}
// 取本地文件信息
monitor.setText("取本地文件信息" + "...");
Map<String, FileObject> localfiles = getLocalfiles();
// 按远程文件信息对比本地文件信息\
monitor.setText("对比文件信息" + "...");
Iterator it = remotefiles.keySet().iterator();
while (it.hasNext()) {
String key = (String) it.next();
if (localfiles.containsKey(key)) {
logger.info("compare file :" + remotefiles.get(key) + ":"
+ localfiles.get(key));
if (localfiles.get(key).size == remotefiles.get(key).size) {
// 存在本地文件且大小相同,从远程文件集中删除
logger.info("remove remote file: " + remotefiles.get(key));
it.remove();
remotefiles.remove(key);
}
}
}
// 下载更新的文件
doDownload(remotefiles);
monitor.setText("退出登录" + "...");
client.disconnect(true);
return true;
}
/**
* 按更新的远程文件集下载文件到当前目录覆盖同名文件
*
* @param remotefiles
* @throws FTPAbortedException
* @throws FTPDataTransferException
* @throws FTPException
* @throws FTPIllegalReplyException
* @throws IOException
* @throws FileNotFoundException
* @throws IllegalStateException
*/
private void doDownload(Map<String, FileObject> remotefiles)
throws IllegalStateException, FileNotFoundException, IOException,
FTPIllegalReplyException, FTPException, FTPDataTransferException,
FTPAbortedException {
for (String key : remotefiles.keySet()) {
monitor.total += remotefiles.get(key).size;
}
monitor.getJpg1().setMaximum(monitor.total);
monitor.setText("开始下载文件" + "...");
int i = 1;
for (String filename : remotefiles.keySet()) {
monitor.getJpg1().setString("下载文件:第" + i + "个/共" + remotefiles.size() + "个");
listener.setFile(remotefiles.get(filename));
client.download(filename, new File(System.getProperty("user.dir") + "/"
+ filename), listener);
i++;
}
}
private TransferMonitor listener = new TransferMonitor(monitor);
private Logger logger = Logger.getLogger(Updater.class);
/**
* 取本地文件夹的所有文件信息
*
* @return
*/
private Map<String, FileObject> getLocalfiles() {
File f = new File(System.getProperty("user.dir"));
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()));
fo.size = file.length();
rtn.put(fo.filename, fo);
logger.info("add local file: " + fo);
}
}
return rtn;
}
/**
* 连接且登录到特定的FTP文件夹
*
* @return
* @throws FTPException
* @throws FTPIllegalReplyException
* @throws IOException
* @throws IllegalStateException
*/
private boolean connectToServer() throws IllegalStateException, IOException,
FTPIllegalReplyException, FTPException {
Properties prop = CommonPropertiesUtil.parseProp("/para.properties");
String url = prop.getProperty("ftpserver", "127.0.0.1");
monitor.setText("连接到系统升级服务器:" + url + "...");
client.connect(url);
String ftpuser = "user";
String ftppwd = "pwd";
monitor.setText("登录中" + "...");
client.login(ftpuser, ftppwd);
String path = prop.getProperty("path", "transms3jars");
monitor.setText("查找目录:" + path + "...");
client.changeDirectory(path);
if (client.isCompressionSupported()) {
logger.info("Compress = TRUE");
client.setCompressionEnabled(true);
}
return true;
}
/**
* 取服务器文件
*
* @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());
o.size = file.getSize();
rtn.put(o.filename, o);
logger.info("add remote file: " + o);
}
return rtn;
}
}
class FileObject {
String filename;
String lasttime;
long size;
@Override
public String toString() {
return filename + "|" + lasttime + "|" + size;
}
}
class TransferMonitor implements FTPDataTransferListener {
private Monitor monitor = null;
private FileObject fo = null;
private Logger logger = Logger.getLogger(Updater.class);
private int size = 0;
public void setFile(FileObject f) {
logger.info("TransferMonitor set file :" + f);
fo = f;
size = 0;
monitor.getJpg2().setMaximum(((Long) fo.size).intValue());
monitor.getJpg2().setValue(0);
monitor.getJpg2().setString("下载中:" + fo.filename);
}
@Override
public void aborted() {
logger.info(fo + " aborted");
}
@Override
public void completed() {
logger.info(fo + " completed");
}
@Override
public void failed() {
logger.info(fo + " failed");
}
@Override
public void started() {
logger.info(fo + " started");
}
@Override
public void transferred(int arg0) {
monitor.getJpg2().setValue(size += arg0);
monitor.getJpg1().setValue(monitor.current += arg0);
}
public TransferMonitor(Monitor monitor) {
super();
this.monitor = monitor;
}
}
进度监控代码:
package cn.stjauns.logan.updater;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JProgressBar;
import javax.swing.SwingConstants;
import java.awt.Color;
public class Monitor extends JFrame {
private static final long serialVersionUID = 1L;
public int total = 0;
public int current = 0;
private JPanel jContentPane = null;
private JLabel jlb = null;
private JProgressBar jpg1 = null;
private JProgressBar jpg2 = null;
/**
* @param owner
*/
public Monitor() {
initialize();
}
public void setText(String text) {
jlb.setText(text);
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setSize(300, 200);
this.setResizable(false);
this.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
this.setTitle("Transms3 AutoUpdater Version 0.1");
this.setContentPane(getJContentPane());
this.setLocationRelativeTo(null);
this.setAlwaysOnTop(true);
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jlb = new JLabel();
jlb.setHorizontalAlignment(SwingConstants.CENTER);
jlb.setForeground(Color.blue);
jlb.setHorizontalTextPosition(SwingConstants.CENTER);
GridLayout gridLayout = new GridLayout();
gridLayout.setRows(3);
jContentPane = new JPanel();
jContentPane.setLayout(gridLayout);
jContentPane.add(jlb, null);
jContentPane.add(getJpg1(), null);
jContentPane.add(getJpg2(), null);
}
return jContentPane;
}
/**
* This method initializes jpg1
*
* @return javax.swing.JProgressBar
*/
protected JProgressBar getJpg1() {
if (jpg1 == null) {
jpg1 = new JProgressBar();
jpg1.setStringPainted(true);
jpg1.setString("总更新文件进度");
}
return jpg1;
}
/**
* This method initializes jpg2
*
* @return javax.swing.JProgressBar
*/
protected JProgressBar getJpg2() {
if (jpg2 == null) {
jpg2 = new JProgressBar();
jpg2.setStringPainted(true);
jpg2.setString("当前更新文件进度");
}
return jpg2;
}
}
源代码见附件。