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
相关推荐
在Java编程环境中,读取FTP(File Transfer Protocol)服务器上的文件是一项常见的任务,尤其是在Web服务器和Linux系统环境下。FTP提供了一种可靠的方式来传输文件,使得应用程序可以远程访问和操作存储在FTP服务器...
FTP服务器的实现还需要日志记录,这里使用了Log4j和SLF4J(Simple Logging Facade for Java)。[log4j-1.2.14.jar]是Log4j的实现,它是一个广泛使用的日志记录框架,允许开发者控制日志输出的级别和格式。SLF4J提供...
【FTP帮助 Java FTP】在Java开发中,FTP(File Transfer Protocol)被广泛用于在本地系统与远程服务器之间传输文件。Apache Commons Net库提供了一个强大的Java FTP客户端实现,使得开发者能够轻松地进行文件上传、...
在Java中实现FTP上传,首先需要创建FTPClient对象,然后连接到FTP服务器,登录后设置工作目录。将要上传的文件转换为二进制流,并调用`storeFile()`方法。注意处理异常和确保文件传输完成后关闭流。 四、FTP下载 ...
for (FTPFile file : files) { System.out.println(file.getName()); } // 改变当前目录 boolean success = ftpClient.changeWorkingDirectory("/public_html"); // 上传文件 InputStream in = new ...
for (FTPFile file : files) { // 检查文件最后修改时间并下载 if (needToUpdate(file)) { client.retrieveFile(file.getName(), new FileOutputStream(file.getName())); } } } catch (Exception e) { e....
Java for FTP server.docx文件可能包含了更具体的代码示例和实现细节,包括如何处理FTP命令、如何进行文件操作等,对于深入学习和实践会有很大帮助。在实际项目中,还可以考虑使用现有的开源FTP服务器库,如Apache ...
在Java编程环境中,FTP(File Transfer Protocol)是一个用于在计算机之间传输文件的标准协议。通过Java实现FTP批量下载文件以及解压的功能,可以极大地提高工作效率,特别是在处理大量数据时。以下是一份详细的步骤...
for (FTPFile file : files) { if (file.getName().equals("desired_file_name")) { OutputStream out = new FileOutputStream("local_file_path"); ftpClient.retrieveFile(file.getName(), out); out.close();...
Java 实现 FTP 自动上传文件是一项常见的任务,尤其在自动化部署、数据同步或者日志传输等场景中。本文将深入探讨如何使用Java编程语言来构建一个FTP文件上传系统,同时结合Log4j日志框架和命令行信息的处理。 首先...
在Java编程中,FTP(File Transfer Protocol)是一个用于在计算机之间传输文件的标准协议。这篇博客“java ftp上传 下载 文件压缩解压”很可能是关于如何使用Java实现FTP文件上传、下载以及文件的压缩与解压功能。...
for (FTPFile file : files) { System.out.println(file.getName()); } ``` ftp4j还支持更复杂的操作,如递归上传/下载整个目录、设置传输模式(二进制或文本)、处理文件权限、以及处理异常情况。 对于需要安全...
Java实现在FTP(文件传输协议)上下载文件是一项常见的任务,尤其在集成系统或服务之间交换数据时。本文将深入探讨如何使用Java编程语言来实现这个功能,同时结合Oracle数据库来获取服务器上的文件地址。 首先,...
Java FTP服务器文件上传下载是Java开发中常见的网络编程任务,主要涉及到FTP(File Transfer Protocol)协议的应用,用于在客户端和服务器之间传输文件。本教程将详细讲解如何使用Java实现FTP服务器的文件上传、下载...
for (FTPFile file : files) { if (file.getType() == FTPFile.FILE_TYPE) { // 文件 String remoteFileName = file.getName(); File localFile = new File(localDir, remoteFileName); try { ftpClient....
for (FTPFile file : files) { // 打印或处理每个文件 } ``` - 切换目录: ```java ftpClient.changeWorkingDirectory(directoryPath); ``` - 创建目录: ```java ftpClient.makeDirectory(directoryPath...
for (FTPFile file : files) { System.out.println(file.getName()); } ftpClient.disconnect(); ``` 2)从FTP服务器上下传一个文件 在`getButton_actionPerformed`方法中,我们同样创建`FtpClient`对象并连接、...
### 使用Java实现FTP服务器 #### 一、简介 在互联网技术的发展过程中,文件传输协议(File Transfer Protocol,简称FTP)是一种用于在网络上进行文件传输的标准服务和协议。它为用户提供了一种简单有效的方式来...
在Java编程中,多线程FTP(File Transfer Protocol)上传和下载是常见的任务,尤其在处理大量文件或者需要并发操作时。在这个场景下,我们通常会利用Java的Apache Commons Net库,它为FTP功能提供了丰富的API。正如...
for (FTPFile file : files) { System.out.println(file.getName()); } ``` ftp4j还支持断点续传,这对于大文件的传输非常有用。`FTPClient`提供了`resumeDownload()`和`resumeUpload()`方法来实现这一功能,这...