n430n00f33 发表于 2016-6-10 00:56:39

ftp4j的使用研究-开源FTP客户端Java类库

ftp4j是一个FTP客户端Java类库,实现了FTP客户端应具有的大部分功能。可以将ftp4j嵌到你的Java应用中,来传输文件(包括上传和下 载),浏览远程FTP服务器上的目录和文件,创建、删除、重命,移动远程目录和文件。ftp4j提供多种方式连接到远程FTP服务器包括:通过 TCP/IP直接连接,通过FTP代理、HTTP代理、SOCKS4/4a代理和SOCKS5代理连接,通过SSL安全连接。
示例代码:
FTPClient client = new FTPClient();
client.connect("ftp.host.com", port);
client.login("carlo", "mypassword");
client.createDirectory("newfolder");
client.disconnect(true);
 
代码实现测试类如下:
 
package it.sauronsoftware.ftp4j;
  
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; 
 
public class MyFTP4jTest2 { 
 
public static void main(String[] args) { 
 
   FTPClient client = new FTPClient(); 
 
   try { 
 
    // 连接 
 
    client.connect("localhost", 2221); 
 
    // 登陆 
 
    client.login("admin", "admin"); 
 
    // 得到当前目录 
 
    String dir = client.currentDirectory(); 
 
    // 列表当前文件 
 
    FTPFile[] files = client.list(); 
 
    if (files != null && files.length > 0) { 
 
     for (int i = 0; i < files.length; i++) { 
 
      FTPFile f = files; 
 
      if (f.getName().equalsIgnoreCase("test1") 
 
        && FTPFile.TYPE_DIRECTORY == f.getType()) { 
 
       //如果目录test1已经有了,那么删除它 
 
       client.deleteDirectory(dir + "/test1"); 
 
       break; 
 
      } 
 
     } 
 
    } 
 
    // 新增目录test1 
 
    client.createDirectory(dir + "/test1"); 
 
    // 进入目录test1 
 
    client.changeDirectory(dir + "/test1"); 
 
    //返回上级目录 
 
    client.changeDirectoryUp(); 
 
    // 上传图片文件 
 
    client.upload(new File("E:\\work\\ftp\\upload\\test2.jpg"),new MyTransferListener()); 
 
    // 上传压缩文件 
 
    client.upload(new File("E:\\work\\ftp\\upload\\test3.rar"),new MyTransferListener()); 
 
    //改名 
 
    client.rename("test2.jpg", "cc.jpg"); 
 
    //移动位置 
 
    client.rename("test3.rar", dir + "/test1/dd.rar"); 
 
    //下载图片 
 
    client.download("cc.jpg", new File("E:\\work\\ftp\\download\\cc.jpg"),new MyTransferListener()); 
 
    //下载压缩文件 
 
    client.download(dir + "/test1/dd.rar", new File("E:\\work\\ftp\\download\\dd.rar"),new MyTransferListener()); 
 
    //删除FTP服务器上文件 
 
    client.deleteFile(dir + "/test1/dd.rar"); 
 
    // 退出 
 
    client.logout(); 
 
    // 释放连接 
 
    client.disconnect(true); 
 
   } catch (IllegalStateException e) { 
 
    e.printStackTrace(); 
 
   } catch (IOException e) { 
 
    e.printStackTrace(); 
 
   } catch (FTPIllegalReplyException e) { 
 
    e.printStackTrace(); 
 
   } catch (FTPException e) { 
 
    e.printStackTrace(); 
 
   } catch (FTPDataTransferException e) { 
 
    e.printStackTrace(); 
 
   } catch (FTPAbortedException e) { 
 
    e.printStackTrace(); 
 
   } catch (FTPListParseException e1) { 
 
    e1.printStackTrace(); 
 
   } 
 

 

 
 
 
下载链接:
 
  http://www.sauronsoftware.it/projects/ftp4j/download.php
 
页: [1]
查看完整版本: ftp4j的使用研究-开源FTP客户端Java类库