• 用edtftpj实现Java FTP客户端工具


    用edtftpj实现Java FTP客户端工具
     
    edtftpj是一个java FTP工具包,使用非常方便,感觉比Apache的好用,但Apache更灵活。edtftpj有多种版本,分别是java、.net和js版本。对于Java版的有一个免费版本。我用的就是免费版本。其余的均为商业版本。
     
    为了开发,先下载免费版本的开发包。
    目前我用的是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)、设置远程主机和用户认证
     
     
    3)、登录到服务器
     
    4)、关闭连接
     
     
    3、设置更多的连接属性
     
    1)、设置自动登录
     
    2)、设置传输缓冲区
     
    3)、设置获取服务器端信息时所使用的编码
     
    4)、设置传输通知的时间间隔
    5)、其他的一些设置
     
    4、改变当前的工作目录
    1)、获取当前的ftp工作目录
     string directory = ftp.getRemoteDirectory();
    2)、改变当前的工作目录
    3)、改变当前的工作目录为其父目录
     
    5、上传与下载、暂停
    1)、上传
     ftp.uploadFile(java.lang.String localFileName, java.lang.String remoteFileName)
    2)、下载
     ftp.downloadFile(java.lang.String localFileName, java.lang.String remoteFileName)
    3)、暂停所有的传输
     
    当然,以上很多方法都有多中重载格式。下面给出一个简单封装的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
     
  • 相关阅读:
    xml解析模块
    python面向对象基础
    python hashlib模块
    os和sys模块
    python反射
    正则表达式re模块
    踩的python列表及for循环一个坑儿
    python序列化模块json和pickle
    python时间模块-time和datetime
    64匹马、8赛道,至少多少轮比赛找出速度最快的4匹马?
  • 原文地址:https://www.cnblogs.com/cuker919/p/4878612.html
Copyright © 2020-2023  润新知