用edtftpj实现Java FTP客户端工具
 
edtftpj是一个java FTP工具包,使用非常方便,感觉比Apache的好用,但Apache更灵活。edtftpj有多种版本,分别是java、.net和js版本。对于Java版的有一个免费版本。我用的就是免费版本。其余的均为商业版本。
 
为了开发,先下载免费版本的开发包。
主  页: [url]http://www.enterprisedt.com/[/url]
工具包: [url]http://www.enterprisedt.com/products/edtftpj/download/edtftpj.zip[/url]
目前我用的是2.03。
 
在开发FTP客户端工具之前,先看看其E文的开发指南:
 
一、开发指南
 
1、在开发中,最核心的API在两个类中,分别是:
 
FileTransferClient  通过构建此对象来是进行FTP文件传输. 支持 "plain" FTP, i.e. RFC 959 FTP 
FTPClient  功能和FileTransferClient一样,已经被废弃. 支持 "plain" FTP, i.e. RFC 959 FTP 
 
2、连接一个FTP服务器
 
1)、构建FileTransferClient对象
 FileTransferClient ftp = new FileTransferClient();
 
2)、设置远程主机和用户认证
  ftp.setRemoteHost(host);
  ftp.setUserName(username);
  ftp.setPassword(password);
 
  ftp.setRemotePort(21);
  ftp.setTimeout(1200);
 
3)、登录到服务器
  ftp.connect();
 
4)、关闭连接
  ftp.disconnect();
 
 
3、设置更多的连接属性
 
1)、设置自动登录
  ftp.getAdvancedFTPSettings().setAutoLogin(false);
 
2)、设置传输缓冲区
  ftp.getAdvancedSettings().setTransferBufferSize(1024)
 
3)、设置获取服务器端信息时所使用的编码
  ftp.getAdvancedSettings().setControlEncoding("GBK");
 
4)、设置传输通知的时间间隔
  ftp.getAdvancedSettings().setTransferNotifyInterval(5000);
5)、其他的一些设置
  ftp.getAdvancedFTPSettings().setConnectMode(FTPConnectMode.ACTIVE);
  ftp.getAdvancedFTPSettings().setActivePortRange(61500, 61510);
  ftp.getAdvancedFTPSettings().setConnectMode(FTPConnectMode.PASV);
 
4、改变当前的工作目录
1)、获取当前的ftp工作目录
 string directory = ftp.getRemoteDirectory();
2)、改变当前的工作目录
  ftp.changeDirectory(directory);
3)、改变当前的工作目录为其父目录
  ftp.changeToParentDirectory();
 
5、上传与下载、暂停
1)、上传
  ftp.uploadFile(java.lang.String localFileName, java.lang.String remoteFileName)
2)、下载
  ftp.downloadFile(java.lang.String localFileName, java.lang.String remoteFileName)
3)、暂停所有的传输
  ftp.cancelAllTransfers()
 
当然,以上很多方法都有多中重载格式。下面给出一个简单封装的JavaFTP客户端工具:
 
二、封装的客户端工具
 
import com.enterprisedt.net.ftp.FTPException;
import com.enterprisedt.net.ftp.FTPFile;
import com.enterprisedt.net.ftp.FileTransferClient;
import com.enterprisedt.net.ftp.WriteMode;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.File;
import java.io.IOException;
import java.text.ParseException;

/**
* edtftpj-2.0.3 工具简单封装测试
*
* @author leizhimin 2008-9-19 13:33:03
*/

public class FtpUtil {
         private static Log log = LogFactory.getLog(FtpUtil. class);
         private FileTransferClient client = null;

         public static void main(String[] args) throws FTPException, IOException {
                FtpUtil ftp = new FtpUtil();
//                ftp.connectServer();
                File file = new File( "C:\\ooo\\upx");
                UploadListener listener = new UploadListener(ftp.client);
                ftp.ftpUploadFolder(file, listener);


                ftp.ftpDownload( "c:\\new1111.gif", "/upx/down.gif");


                ftp.disconnect();
        }

         public FtpUtil() {
                 this.connectServer();
        }

         public FileTransferClient getClient() {
                 return client;
        }

         /**
         * 连接ftp服务器
         *
         * @return 连接成功标识
         */

         public boolean connectServer() {
                 boolean flag = false;
                client = new FileTransferClient();
                 try {
                        client.setUserName( "admin");
                        client.setPassword( "123");
                        client.setRemoteHost( "localhost");
                        client.setRemotePort(21);
                        client.setTimeout(1200);

                        client.setEventListener( new UploadListener(client));
                        client.getAdvancedSettings().setTransferBufferSize(1000);
                        client.getAdvancedSettings().setTransferNotifyInterval(5000);
                        client.getAdvancedSettings().setControlEncoding( "GBK");
                        client.connect();
                        flag = true;

                } catch (FTPException e) {
                        e.printStackTrace();
                } catch (IOException e) {
                        e.printStackTrace();
                }
                 return flag;
        }

         /**
         * 关闭FTP客户端连接
         *
         * @return 关闭成功标识
         */

         public boolean disconnect() {
                 boolean flag = false;
                 try {
                        client.disconnect();
                        flag = true;
                } catch (FTPException e) {
                        e.printStackTrace();
                } catch (IOException e) {
                        e.printStackTrace();
                }
                 return flag;
        }

         /**
         * 上传文件
         *
         * @param file 文件
         * @param ul     监听器
         */

         public void ftpUploadFile(File file, UploadListener ul) {
                String fileName = file.getName();
                String filePath = file.getPath();
                 try {
                        client.uploadFile(filePath, fileName, WriteMode.RESUME);
                         int len = ( int) client.getSize(fileName);
                        log.info( "上传文件" + filePath + "完成,大小为" + len + "字节!");
                } catch (FTPException e) {
                        e.printStackTrace();
                } catch (IOException e) {
                        e.printStackTrace();
                }
        }

         /**
         * 上传文件(夹)
         *
         * @param file 上传文件
         * @param ul     上传监听器
         */

         public void ftpUploadFolder(File file, UploadListener ul) {
                 if (file.isFile()) {
                        ftpUploadFile(file, ul);
                }
                 try {
                         if (file.isDirectory()) {
                                FTPFile[] ftpFiles = client.directoryList();
                                 boolean isExist = false;
                                 //遍历FTP当前目录的文件文件名,如果存在则停止查找,如果不存在则设计标记为不存在
                                 for (FTPFile ftpFile : ftpFiles) {
                                         if (ftpFile.getName().equals(file.getName())) {
                                                isExist = true;
                                                 break;
                                        }
                                }
                                 //如果要上传的目录不存在,则创建上传目录
                                 if (!isExist) {
                                        client.createDirectory(file.getName());
                                }
                                 //设置当前目录 
                                client.changeDirectory(file.getName());

                                 //上传文件的列表
                                File[] upFiles = file.listFiles();
                                 for (File upFile : upFiles) {
                                         if (upFile.isFile()) {
                                                ftpUploadFile(upFile, ul);
                                        } else if (upFile.isDirectory()) {
                                                ftpUploadFolder(upFile, ul);
                                        }
                                }
                                client.changeToParentDirectory();
                        }
                } catch (FTPException e) {
                        e.printStackTrace();
                } catch (IOException e) {
                        e.printStackTrace();
                } catch (ParseException e) {
                        e.printStackTrace();
                }
        }

         /**
         * 下载文件
         *
         * @param localFileName    本地文件名
         * @param remoteFileName 远程文件名
         * @throws FTPException
         * @throws IOException
         */

         public void ftpDownload(String localFileName, String remoteFileName) throws FTPException, IOException {
                client.downloadFile(localFileName, remoteFileName, WriteMode.OVERWRITE);
        }
}
 
import com.enterprisedt.net.ftp.EventAdapter;
import com.enterprisedt.net.ftp.FileTransferClient;

/**
* 上传过程监听类
*
* @author leizhimin 2008-9-22 16:05:53
*/

class UploadListener extends EventAdapter {
         private long bytesTransferred = 0;
         private FileTransferClient ftpClient;

         public UploadListener(FileTransferClient ftpClient) {
                 this.ftpClient = ftpClient;
        }

         public void bytesTransferred(String connId, String remoteFilename, long bytes) {
                bytesTransferred = bytes;
        }
}
 
运行结果:
2008-9-22 17:45:48 zzvcom.cms.ccm.jmkws.common.FtpUtil ftpUploadFile
信息: 上传文件C:\ooo\upx\1\12\12.txt完成,大小为0字节!
2008-9-22 17:45:48 zzvcom.cms.ccm.jmkws.common.FtpUtil ftpUploadFile
信息: 上传文件C:\ooo\upx\1\11\11.txt完成,大小为0字节!
2008-9-22 17:45:48 zzvcom.cms.ccm.jmkws.common.FtpUtil ftpUploadFile
信息: 上传文件C:\ooo\upx\1\1.txt完成,大小为0字节!
2008-9-22 17:45:48 zzvcom.cms.ccm.jmkws.common.FtpUtil ftpUploadFile
信息: 上传文件C:\ooo\upx\1\新建 FDF Document.fdf完成,大小为0字节!
2008-9-22 17:45:48 zzvcom.cms.ccm.jmkws.common.FtpUtil ftpUploadFile
信息: 上传文件C:\ooo\upx\1\新建 WinRAR 压缩文件.rar完成,大小为20字节!
2008-9-22 17:45:48 zzvcom.cms.ccm.jmkws.common.FtpUtil ftpUploadFile
信息: 上传文件C:\ooo\upx\aa.fdf完成,大小为0字节!

Process finished with exit code 0
 
 
以上代码上传支持文件夹操作。