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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ftp4j是一个FTP客户端Java类库,实现了FTP客户端应具有的大部分功能。可以将ftp4j嵌到你的Java应用中,来传输文件(包括上传和下载),浏览远程FTP服务器上的目录和文件,创建、删除、重命,移动远程目录和文件。ftp4j提供多种方式连接到远程FTP服务器包括:通过 TCP/IP直接连接,通过FTP代理、HTTP代理、SOCKS4/4a代理和SOCKS5代理连接,通过SSL安全连接。 简单应用---API介绍 The main class of the library is FTPClient (it.sauronsoftware.ftp4j.FTPClient). 1. 创建FTPClient实例 FTPClient client = new FTPClient(); 连接到指定的FTP服务器(域名或IP) 不指定端口,则使用默认端口21 client.connect("ftp.host.com", /*21*/); 登录验证 client.login("user", "pswd"); 下面是匿名登录 //client.login("anonymous", "密码任意设置"); client.login("anonymous", "ftp4j"); 安全退出 client.disconnect(true); //强制退出 //client.disconnect(false); 文件以及文件夹操作: 取得当前文件夹 String dir = client.currentDirectory(); 改变当前文件夹 client.changeDirectory(newPath); //绝对路径 //client.changeDirectory("/an/absolute/one"); //相对路径 //client.changeDirectory("relative"); //回退到上级目录 client.changeDirectoryUp(); //重命名文件或文件夹 client.rename("oldname", "newname"); //移动文件或文件夹 client.rename("myfile.txt", "myfolder/myfile.txt"); //删除文件 client.deleteFile(relativeOrAbsolutePath); //client.deleteFile("useless.txt"); //创建目录 client.createDirectory("newfolder"); //删除目录(空目录) client.deleteDirectory(absoluteOrRelativePath); //client.deleteDirectory("oldfolder"); //浏览文件 FTPFile[] list = client.list(); //使用通配浏览文件 FTPFile[] list = client.list("*.jpg"); //显示文件或文件夹的修改时间 java.util.Date md = client.modifiedDate("filename.ext"); //上传和下载文件 //下载服务器上remoteFile.ext 下载到本地 localFile.ext client.download("remoteFile.ext", new java.io.File("localFile.ext")); //上传 本地 localFile.ext 到当前目录 client.upload(new java.io.File("localFile.ext")); // 上传和下载文件时, 监听文件传输的状态 public class MyTransferListener implements FTPDataTransferListener { //文件开始上传或下载时触发 public void started() { } //显示已经传输的字节数 public void transferred(int length) { } //文件传输完成时,触发 public void completed() { } //传输放弃时触发 public void aborted() { // Transfer aborted } //传输失败时触发 public void failed() { } 示例: client.download("remoteFile.ext", new java.io.File("localFile.ext"), new MyTransferListener()); client.upload(new java.io.File("localFile.ext"), new MyTransferListener()); //ftp4j也支持断点续传功能 下面是一个简单示例:*参数 1056 跳过 1056字节点下载 client.download("remoteFile.ext", new java.io.File("localFile.ext"), 1056); 设置传输模式 //ASC码 client.setType(FTPClient.TYPE_TEXTUAL); //二进制 client.setType(FTPClient.TYPE_BINARY); //自动选择(根据文件内容) client.setType(FTPClient.TYPE_AUTO); //设置连接器 client.setConnector(connector); SSL 套接字连接 client.setConnector(it.sauronsoftware.ftp4j.connectors.SSLConnector) client.setConnector(anyConnectorYouWant);
你可以按照以下步骤在CentOS上安装FTP服务器: 1. 打开终端并使用root用户登录。 2. 运行以下命令更新系统软件包: ``` yum update ``` 3. 使用以下命令安装vsftpd软件包: ``` yum install vsftpd ``` 4. 安装完成后,使用以下命令启动FTP服务: ``` systemctl start vsftpd ``` 5. 若要设置FTP服务在系统启动时自动启动,请运行以下命令: ``` systemctl enable vsftpd ``` 6. 默认情况下,FTP服务器的根目录位于`/var/ftp`。您可以将文件上传到此目录,或者根据需要更改FTP服务器的配置。 7. 如果您的CentOS服务器上启用了防火墙,您需要开放FTP服务的相关端口。默认情况下,FTP使用TCP端口21用于控制连接,以及TCP端口范围20-21用于数据传输。您可以通过编辑防火墙配置文件来打开这些端口: ``` vi /etc/sysconfig/iptables ``` 8. 添加以下规则以允许FTP流量通过防火墙: ``` -A INPUT -m state --state NEW -m tcp -p tcp --dport 20:21 -j ACCEPT -A INPUT -m state --state NEW -m tcp -p tcp --dport 1024:65535 -j ACCEPT ``` 9. 保存并关闭文件。然后重新启动防火墙以使更改生效: ``` systemctl restart iptables ``` 现在,您的CentOS服务器上应该可以访问FTP服务了。您可以使用FTP客户端连接到服务器并进行文件传输。请注意,上述步骤仅涵盖了最基本的FTP服务器安装和配置,您可能需要进一步调整和安全配置以满足您的特定需求。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值