在 Java 中实现 FTP 文件上传和下载,有几种常见的方式。我将为您介绍使用 Apache Commons Net 库和 Java 内置的 FTP 相关类的两种方法。以下是每种方法的详细步骤流程、依赖坐标和示例代码。
Apache Commons Net 库是一个广泛使用的库,用于处理网络协议。在这个库中,有一个专门用于 FTP 操作的模块,可以方便地实现 FTP 文件上传和下载。
步骤流程:
引入 Maven 依赖:
<!-- Maven依赖 -->
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.8.0</version>
</dependency>
实现 FTP 文件上传:
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class FTPUploader {
public static void main(String[] args) {
String server = "ftp.example.com";
int port = 21;
String username = "your-username";
String password = "your-password";
FTPClient ftpClient = new FTPClient();
try {
ftpClient.connect(server, port);
ftpClient.login(username, password);
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
File localFile = new File("local-file.txt");
String remoteFilePath = "/remote-folder/remote-file.txt";
FileInputStream inputStream = new FileInputStream(localFile);
ftpClient.storeFile(remoteFilePath, inputStream);
inputStream.close();
ftpClient.logout();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ftpClient.isConnected()) {
try {
ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
实现 FTP 文件下载:
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
public class FTPDownloader {
public static void main(String[] args) {
String server = "ftp.example.com";
int port = 21;
String username = "your-username";
String password = "your-password";
FTPClient ftpClient = new FTPClient();
try {
ftpClient.connect(server, port);
ftpClient.login(username, password);
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
String remoteFilePath = "/remote-folder/remote-file.txt";
File localFile = new File("local-file.txt");
FileOutputStream outputStream = new FileOutputStream(localFile);
InputStream inputStream = ftpClient.retrieveFileStream(remoteFilePath);
byte[] bytes = new byte[1024];
int bytesRead;
while ((bytesRead = inputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, bytesRead);
}
inputStream.close();
outputStream.close();
ftpClient.logout();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ftpClient.isConnected()) {
try {
ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
Java 内置了一些用于 FTP 操作的类,虽然不如 Apache Commons Net 库功能丰富,但也能满足基本需求。
步骤流程:
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.SocketException;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
public class JavaFTPUploder {
public static void main(String[] args) {
String server = "ftp.example.com";
int port = 21;
String username = "your-username";
String password = "your-password";
FTPClient ftpClient = new FTPClient();
try {
ftpClient.connect(server, port);
ftpClient.login(username, password);
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
File localFile = new File("local-file.txt");
String remoteFilePath = "/remote-folder/remote-file.txt";
FileInputStream inputStream = new FileInputStream(localFile);
OutputStream outputStream = ftpClient.storeFileStream(remoteFilePath);
byte[] bytes = new byte[1024];
int bytesRead;
while ((bytesRead = inputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, bytesRead);
}
inputStream.close();
outputStream.close();
ftpClient.completePendingCommand();
ftpClient.logout();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ftpClient.isConnected()) {
try {
ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
实现 FTP 文件下载:
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
public class JavaFTPDownloader {
public static void main(String[] args) {
String server = "ftp.example.com";
int port = 21;
String username = "your-username";
String password = "your-password";
FTPClient ftpClient = new FTPClient();
try {
ftpClient.connect(server, port);
ftpClient.login(username, password);
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
String remoteFilePath = "/remote-folder/remote-file.txt";
InputStream inputStream = ftpClient.retrieveFileStream(remoteFilePath);
FileOutputStream outputStream = new FileOutputStream("local-file.txt");
byte[] bytes = new byte[1024];
int bytesRead;
while ((bytesRead = inputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, bytesRead);
}
inputStream.close();
outputStream.close();
ftpClient.completePendingCommand();
ftpClient.logout();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ftpClient.isConnected()) {
try {
ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
请注意,以上示例代码中的"ftp.example.com"、"your-username"和"your-password"需要替换为您实际的 FTP 服务器信息和凭证。此外,这些示例主要展示了基本的 FTP 文件上传和下载操作,实际应用中可能需要处理更多的异常情况和错误处理。