ftp4j

/**   
 
*  
 
* @Description: TODO
 
* @author ZhangHuan   
 
* @date 2014-3-2 上午10:22:30 
 
* @version V1.0   
 
*/ 
package com.zh;


/**  
 * @Description: TODO 
 * @author ZhangHuan  
 * @date 2014-3-2 上午10:22:30    
 */


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 it.sauronsoftware.ftp4j.connectors.FTPProxyConnector;
import it.sauronsoftware.ftp4j.connectors.SOCKS4Connector;




import java.io.File; 
import java.io.IOException;
import java.util.List; 
public final class FTPToolkit { 


        private FTPToolkit() { 
        } 


        /** 
         * 创建FTP连接 
         * 
         * @param host         主机名或IP 
         * @param port         ftp端口 
         * @param username ftp用户名 
         * @param password ftp密码 
         * @return 一个客户端 
         * @throws FTPException 
         * @throws FTPIllegalReplyException 
         * @throws IOException 
         * @throws IllegalStateException 
         * @throws FTPListParseException 
         * @throws FTPAbortedException 
         * @throws FTPDataTransferException 
         */ 
        public static FTPClient makeFtpConnection(String host, int port, String username, String password,String prosyIp,int proxyPort) throws IllegalStateException, IOException, FTPIllegalReplyException, FTPException, FTPDataTransferException, FTPAbortedException, FTPListParseException { 
                FTPClient client = new FTPClient(); 
                //FTPProxyConnector proxy=new FTPProxyConnector(host, port, username, password);
                SOCKS4Connector socks4 = new SOCKS4Connector(prosyIp, proxyPort);
                client.setConnector(socks4);
                client.connect(host, port);
                client.login(username, password);
                client.setCharset("gbk");


              //list files


              FTPFile[] list = client.list();


              for(FTPFile file : list) {


              System.out.println(file);


              }


              client.disconnect(true);
                //roxy.setStyle()
                try { 
                        client.connect(host, port); 
                        client.login(username, password); 
                } catch (Exception e) { 
                        throw new FTPRuntimeException(e); 
                } 
                return client; 
        } 


        /** 
         * FTP下载文件到本地一个文件夹,如果本地文件夹不存在,则创建必要的目录结构 
         * 
         * @param client                    FTP客户端 
         * @param remoteFileName    FTP文件 
         * @param localFolderPath 存的本地目录 
         */ 
        public static void download(FTPClient client, String remoteFileName, String localFolderPath) { 
                int x = isExist(client, remoteFileName); 
                MyFtpListener listener = MyFtpListener.instance(FTPOptType.UP); 
                File localFolder = new File(localFolderPath); 
                if (localFolder.isFile()) { 
                        throw new FTPRuntimeException("所要的下载保存的地方是一个文件,无法保存!"); 
                } else { 
                        if (!localFolder.exists()) 
                                localFolder.mkdirs(); 
                } 
                if (x == FTPFile.TYPE_FILE) { 
                        String localfilepath = PathToolkit.formatPath4File(localFolderPath + File.separator + new File(remoteFileName).getName()); 
                        try { 
                                if (listener != null) 
                                        client.download(remoteFileName, new File(localfilepath), listener); 
                                else 
                                        client.download(remoteFileName, new File(localfilepath)); 
                        } catch (Exception e) { 
                                throw new FTPRuntimeException(e); 
                        } 
                } else { 
                        throw new FTPRuntimeException("所要下载的文件" + remoteFileName + "不存在!"); 
                } 
        } 


        /** 
         * FTP上传本地文件到FTP的一个目录下 
         * 
         * @param client                     FTP客户端 
         * @param localfile                本地文件 
         * @param remoteFolderPath FTP上传目录 
         */ 
        public static void upload(FTPClient client, File localfile, String remoteFolderPath) { 
                remoteFolderPath = PathToolkit.formatPath4FTP(remoteFolderPath); 
                MyFtpListener listener = MyFtpListener.instance(FTPOptType.UP); 
                try { 
                        client.changeDirectory(remoteFolderPath); 
                        if (!localfile.exists()) throw new FTPRuntimeException("所要上传的FTP文件" + localfile.getPath() + "不存在!"); 
                        if (!localfile.isFile()) throw new FTPRuntimeException("所要上传的FTP文件" + localfile.getPath() + "是一个文件夹!"); 
                        if (listener != null) 
                                client.upload(localfile, listener); 
                        else 
                                client.upload(localfile); 
                        client.changeDirectory("/"); 
                } catch (Exception e) { 
                        throw new FTPRuntimeException(e); 
                } 
        } 


        /** 
         * FTP上传本地文件到FTP的一个目录下 
         * 
         * @param client                     FTP客户端 
         * @param localfilepath        本地文件路径 
         * @param remoteFolderPath FTP上传目录 
         */ 
        public static void upload(FTPClient client, String localfilepath, String remoteFolderPath) { 
                File localfile = new File(localfilepath); 
                upload(client, localfile, remoteFolderPath); 
        } 


        /** 
         * 批量上传本地文件到FTP指定目录上 
         * 
         * @param client                     FTP客户端 
         * @param localFilePaths     本地文件路径列表 
         * @param remoteFolderPath FTP上传目录 
         */ 
        public static void uploadListPath(FTPClient client, List<String> localFilePaths, String remoteFolderPath) { 
                remoteFolderPath = PathToolkit.formatPath4FTP(remoteFolderPath); 
                try { 
                        client.changeDirectory(remoteFolderPath); 
                        MyFtpListener listener = MyFtpListener.instance(FTPOptType.UP); 
                        for (String path : localFilePaths) { 
                                File file = new File(path); 
                                if (!file.exists()) throw new FTPRuntimeException("所要上传的FTP文件" + path + "不存在!"); 
                                if (!file.isFile()) throw new FTPRuntimeException("所要上传的FTP文件" + path + "是一个文件夹!"); 
                                if (listener != null) 
                                        client.upload(file, listener); 
                                else 
                                        client.upload(file); 
                        } 
                        client.changeDirectory("/"); 
                } catch (Exception e) { 
                        throw new FTPRuntimeException(e); 
                } 
        } 


        /** 
         * 批量上传本地文件到FTP指定目录上 
         * 
         * @param client                     FTP客户端 
         * @param localFiles             本地文件列表 
         * @param remoteFolderPath FTP上传目录 
         */ 
        public static void uploadListFile(FTPClient client, List<File> localFiles, String remoteFolderPath) { 
                try { 
                        client.changeDirectory(remoteFolderPath); 
                        remoteFolderPath = PathToolkit.formatPath4FTP(remoteFolderPath); 
                        MyFtpListener listener = MyFtpListener.instance(FTPOptType.UP); 
                        for (File file : localFiles) { 
                                if (!file.exists()) throw new FTPRuntimeException("所要上传的FTP文件" + file.getPath() + "不存在!"); 
                                if (!file.isFile()) throw new FTPRuntimeException("所要上传的FTP文件" + file.getPath() + "是一个文件夹!"); 
                                if (listener != null) 
                                        client.upload(file, listener); 
                                else 
                                        client.upload(file); 
                        } 
                        client.changeDirectory("/"); 
                } catch (Exception e) { 
                        throw new FTPRuntimeException(e); 
                } 
        } 




        /** 
         * 判断一个FTP路径是否存在,如果存在返回类型(FTPFile.TYPE_DIRECTORY=1、FTPFile.TYPE_FILE=0、FTPFile.TYPE_LINK=2) 
         * 如果文件不存在,则返回一个-1 
         * 
         * @param client         FTP客户端 
         * @param remotePath FTP文件或文件夹路径 
         * @return 存在时候返回类型值(文件0,文件夹1,连接2),不存在则返回-1 
         */ 
        public static int isExist(FTPClient client, String remotePath) { 
                remotePath = PathToolkit.formatPath4FTP(remotePath); 
                int x = -1; 
                FTPFile[] list = null; 
                try { 
                        list = client.list(remotePath); 
                } catch (Exception e) { 
                        return -1; 
                } 
                if (list.length > 1) return FTPFile.TYPE_DIRECTORY; 
                else if (list.length == 1) { 
                        FTPFile f = list[0]; 
                        if (f.getType() == FTPFile.TYPE_DIRECTORY) return FTPFile.TYPE_DIRECTORY; 
                        //假设推理判断 
                        String _path = remotePath + "/" + f.getName(); 
                        try { 
                                int y = client.list(_path).length; 
                                if (y == 1) return FTPFile.TYPE_DIRECTORY;  
                                else return FTPFile.TYPE_FILE; 
                        } catch (Exception e) { 
                                return FTPFile.TYPE_FILE; 
                        } 
                } else { 
                        try { 
                                client.changeDirectory(remotePath); 
                                return FTPFile.TYPE_DIRECTORY; 
                        } catch (Exception e) { 
                                return -1; 
                        } 
                } 
        } 


        /** 
         * 关闭FTP连接,关闭时候像服务器发送一条关闭命令 
         * 
         * @param client FTP客户端 
         * @return 关闭成功,或者链接已断开,或者链接为null时候返回true,通过两次关闭都失败时候返回false 
         */ 


        public static boolean closeConnection(FTPClient client) { 
                if (client == null) return true; 
                if (client.isConnected()) { 
                        try { 
                                client.disconnect(true); 
                                return true; 
                        } catch (Exception e) { 
                                try { 
                                        client.disconnect(false); 
                                } catch (Exception e1) { 
                                        e1.printStackTrace(); 
                                        return false; 
                                } 
                        } 
                } 
                return true; 
        } 
}
/**   
 
* @Title: dd.java 
 
* @Description: TODO
 
* @author ZhangHuan   
 
* @date 2014-3-2 上午10:27:21 
 
* @version V1.0   
 
*/ 
package com.zh;


/**  
 * @Description: TODO 
 * @author ZhangHuan  
 * @date 2014-3-2 上午10:27:21    
 */


public class FTPRuntimeException extends RuntimeException { 
        /**  
 * @Fields serialVersionUID : TODO 
 */
private static final long serialVersionUID = 1L;


public FTPRuntimeException() { 
                super(); 
        } 


        public FTPRuntimeException(String message) { 
                super(message); 
        } 


        public FTPRuntimeException(String message, Throwable cause) { 
                super(message, cause); 
        } 


        public FTPRuntimeException(Throwable cause) { 
                super(cause); 
        } 
}/**   
 
*
 
* @Description: TODO
 
* @author ZhangHuan   
 
* @date 2014-3-2 上午10:28:25 
 
* @version V1.0   
 
*/ 
package com.zh;


import it.sauronsoftware.ftp4j.FTPDataTransferListener;


/**  
 * @Description: TODO 
 * @author ZhangHuan  
 * @date 2014-3-2 上午10:28:25    
 */
public class MyFtpListener implements FTPDataTransferListener { 
        private FTPOptType optType; 


        public static MyFtpListener instance(FTPOptType optType) { 
                return new MyFtpListener(optType); 
        } 


        private MyFtpListener(FTPOptType optType) { 
                this.optType = optType; 
        } 


        public void started() { 
                System.out.println(optType.getOptname() + ":FTP启动喽。。。。。。"); 
        } 


        public void transferred(int length) { 
                System.out.println(optType.getOptname() + ":FTP传输喽。。。。。。"); 


        } 


        public void completed() { 
                System.out.println(optType.getOptname() + ":FTP完成喽。。。。。。"); 
        } 


        public void aborted() { 
                System.out.println(optType.getOptname() + ":FTP中止喽。。。。。。"); 
        } 


        public void failed() { 
                System.out.println(optType.getOptname() + ":FTP挂掉喽。。。。。。"); 
        } 
}
 /**   
 
* @Title: CC.java 
 
* @Description: TODO
 
* @author ZhangHuan   
 
* @date 2014-3-2 上午10:26:20 
 
* @version V1.0   
 
*/ 
package com.zh;


/**  
 * @Description: TODO 
 * @author ZhangHuan  
 * @date 2014-3-2 上午10:26:20    
 */


import java.io.File; 


/** 
* 路径处理工具,操作系统自适应 


*/ 
public final class PathToolkit { 
        private PathToolkit() { 
        } 


        /** 
         * 格式化文件路径,将其中不规范的分隔转换为标准的分隔符,并且去掉末尾的文件路径分隔符。 
         * 本方法操作系统自适应 
         * 
         * @param path 文件路径 
         * @return 格式化后的文件路径 
         */ 
        public static String formatPath4File(String path) { 
                String reg0 = "\\\\+"; 
                String reg = "\\\\+|/+"; 
                String temp = path.trim().replaceAll(reg0, "/"); 
                temp = temp.replaceAll(reg, "/"); 
                if (temp.length() > 1 && temp.endsWith("/")) { 
                        temp = temp.substring(0, temp.length() - 1); 
                } 
                temp = temp.replace('/', File.separatorChar); 
                return temp; 
        } 


        /** 
         * 格式化文件路径,将其中不规范的分隔转换为标准的分隔符 
         * 并且去掉末尾的"/"符号(适用于FTP远程文件路径或者Web资源的相对路径)。 
         * 
         * @param path 文件路径 
         * @return 格式化后的文件路径 
         */ 
        public static String formatPath4FTP(String path) { 
                String reg0 = "\\\\+"; 
                String reg = "\\\\+|/+"; 
                String temp = path.trim().replaceAll(reg0, "/"); 
                temp = temp.replaceAll(reg, "/"); 
                if (temp.length() > 1 && temp.endsWith("/")) { 
                        temp = temp.substring(0, temp.length() - 1); 
                } 
                return temp; 
        } 


        /** 
         * 获取FTP路径的父路径,但不对路径有效性做检查 
         * 
         * @param path FTP路径 
         * @return 父路径,如果没有父路径,则返回null 
         */ 
        public static String genParentPath4FTP(String path) { 
                String pp = new File(path).getParent(); 
                if (pp == null) return null; 
                else return formatPath4FTP(pp); 
        } 
}/**   
 
* @Title: ss.java 
 
* @Description: TODO
 
* @author ZhangHuan   
 
* @date 2014-3-2 上午10:30:46 
 
* @version V1.0   
 
*/ 
package com.zh;


/**  
 * @Description: TODO 
 * @author ZhangHuan  
 * @date 2014-3-2 上午10:30:46    
 */


import it.sauronsoftware.ftp4j.FTPClient; 




/** 
* 简单测试下 

*
*/ 
public class Test { 
        public static void main(String args[]) throws Exception { 
                String ftpip = "192.168.30.17"; 
                int ftpport = 21; 
                String ftpuser = "zhanghuan"; 
                String ftppswd = "111111"; 
                String proxyIp="127.0.0.1";
                int proxyPort=1080;
                FTPClient client = FTPToolkit.makeFtpConnection(ftpip, ftpport, ftpuser, ftppswd,proxyIp,proxyPort);
                
                FTPToolkit.upload(client, "C:\\Dynamicclrr4.zip", "/aaa/bbb/ccc"); 
                FTPToolkit.download(client, "/Dynamicclrr4.zip", "D:\\"); 
                FTPToolkit.closeConnection(client); 
        } 
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值