mupyfrkpqd 发表于 2016-6-7 12:01:30

ftp4j之FTP

  The ftp4j library implements a Java full-features FTP client. With ftp4j embedded in your application you can: transfer files (upload and download), browse the remote FTP site (directory listing included), create, delete, rename and move remote directories and files.
  
  ftp4j类库可支持实现java版的FTP客户端,可应用到你的应用程序中,实现文件的上传下载,浏览远程FTP服务器上的目录和文件,创建、删除、重命,移动远程目录和文件等操作。
  
  ftp4j的特点:
  
  1、100%免费
  2、可远程连接FTP服务器
  3、远程操作
  4、文件上传下载
  5、异常捕获
  
  下载路径:http://www.sauronsoftware.it/projects/ftp4j/ftp4j-1.3.1.zip
  
  快速上手:
  
  The main class of the library is FTPClient (it.sauronsoftware.ftp4j.FTPClient).
  Start creating a FTPClient instance:

FTPClient client = new FTPClient();
  Connect now to a remote FTP service:

client.connect("ftp.host.com");
  If the service port is other than the standard 21:

client.connect("ftp.host.com", port);
  In example:

client.connect("ftp.host.com", 8021);
  Step now to the login procedure:

client.login("carlo", "mypassword");
  If no exception is thrown you are now authenticated to the remote server. Otherwise, if the authentication attempt fails, you receive a it.sauronsoftware.ftp4j.FTPException.
  Anonymous authentication, if admitted by the connected service, can be done sending the username "anonymous" and an arbitrary password (note that some servers require an e-mail address in place of the password):

client.login("anonymous", "ftp4j");
  Do anything you want with the remote FTP service, then disconnect:

client.disconnect(true);
  This one sends the FTP QUIT command to the remote server, requesting a legal disconnect procedure. If you just want to break the connection, without sending any advice to the server, call:

client.disconnect(false);
  

Connecting through a proxy
  The client connects to the server through a connector (an object implementing the it.sauronsoftware.ftp4j.FTPConnector interface), which returns to the client an already open connection (an object implementing the it.sauronsoftware.ftp4j.FTPConnection interface). That is why ftp4j could support a large set of proxies.
  The connector for a client instance can be setted with the setConnector() method, obviously before connecting the remote server:

client.setConnector(anyConnectorYouWant);
Browsing the remote site
  Get the current directory absolute path calling:

String dir = client.currentDirectory();
  Change directory with:

client.changeDirectory(newPath);
  You can use both absolute and relative paths:

client.changeDirectory("/an/absolute/one");
client.changeDirectory("relative");
  Back to the parent directory with:

client.changeDirectoryUp();
  

Renaming files and directories
  To rename a remote file or directory:

client.rename("oldname", "newname");
  

Moving files and directories
  The rename() method can also be used to move files and directories from a location to another.
  In example, think in the current working directory you have a file called "myfile.txt", and you want to move it in the sub-directory "myfolder":

client.rename("myfile.txt", "myfolder/myfile.txt");
  

Deleting files
  To delete a remote file call:

client.deleteFile(relativeOrAbsolutePath);
  In example:

client.deleteFile("useless.txt");
  

Creating and deleting directories
  You can create a new directory on the remote site, if the service gives you this oppurtunity:

client.createDirectory("newfolder");
  You can also remove an existing one:

client.deleteDirectory(absoluteOrRelativePath);
  In example:

client.deleteDirectory("oldfolder");
  Please note that usually FTP servers can delete only empty directories.
  
  

Downloading and uploading files
  The easiest way to download a remote file is a call to the download(String, File) method:

client.download("remoteFile.ext", new java.io.File("localFile.ext"));
  To upload:

client.upload(new java.io.File("localFile.ext"));
  
  
  参考文章路径:http://www.sauronsoftware.it/projects/ftp4j/manual.php#3
页: [1]
查看完整版本: ftp4j之FTP