Java 获取 FTP 文件大小
在 Java 中,你可以使用 Apache Commons Net 库来获取 FTP 服务器上文件的大小,以下是几种实现方法:

使用 Apache Commons Net (推荐)
确保你添加了 Commons Net 依赖:
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.9.0</version>
</dependency>
然后使用以下代码获取文件大小:
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import java.io.IOException;
public class FtpFileSizeExample {
public static long getFtpFileSize(String server, int port, String username,
String password, String remoteFilePath) throws IOException {
FTPClient ftpClient = new FTPClient();
try {
// 连接FTP服务器
ftpClient.connect(server, port);
int reply = ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
throw new IOException("FTP server refused connection.");
}
// 登录
if (!ftpClient.login(username, password)) {
throw new IOException("FTP login failed.");
}
// 设置文件类型为二进制
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
// 获取文件大小
return ftpClient.listFiles(remoteFilePath)[0].getSize();
} finally {
// 断开连接
if (ftpClient.isConnected()) {
ftpClient.logout();
ftpClient.disconnect();
}
}
}
public static void main(String[] args) {
String server = "ftp.example.com";
int port = 21;
String username = "your_username";
String password = "your_password";
String remoteFilePath = "/path/to/your/file.txt";
try {
long fileSize = getFtpFileSize(server, port, username, password, remoteFilePath);
System.out.println("文件大小: " + fileSize + " 字节");
} catch (IOException e) {
e.printStackTrace();
}
}
}
使用 SIZE 命令
如果FTP服务器支持SIZE命令,可以直接使用:
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import java.io.IOException;
public class FtpFileSizeWithSizeCommand {
public static long getFtpFileSizeWithSizeCommand(String server, int port, String username,
String password, String remoteFilePath) throws IOException {
FTPClient ftpClient = new FTPClient();
try {
ftpClient.connect(server, port);
int reply = ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
throw new IOException("FTP server refused connection.");
}
if (!ftpClient.login(username, password)) {
throw new IOException("FTP login failed.");
}
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
// 使用SIZE命令获取文件大小
return ftpClient.size(remoteFilePath);
} finally {
if (ftpClient.isConnected()) {
ftpClient.logout();
ftpClient.disconnect();
}
}
}
public static void main(String[] args) {
String server = "ftp.example.com";
int port = 21;
String username = "your_username";
String password = "your_password";
String remoteFilePath = "/path/to/your/file.txt";
try {
long fileSize = getFtpFileSizeWithSizeCommand(server, port, username, password, remoteFilePath);
System.out.println("文件大小: " + fileSize + " 字节");
} catch (IOException e) {
e.printStackTrace();
}
}
}
注意事项
- 不是所有FTP服务器都支持SIZE命令,方法一更通用
- 确保你有足够的权限访问该文件
- 对于大文件,可能需要考虑超时设置
- 如果文件不存在,
listFiles()会返回null或空数组,需要处理这种情况 - 对于被动模式(Passive Mode),可能需要额外配置
处理异常情况的代码示例
public static long getFtpFileSizeWithExceptionHandling(String server, int port, String username,
String password, String remoteFilePath) throws IOException {
FTPClient ftpClient = new FTPClient();
try {
ftpClient.connect(server, port);
if (!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
throw new IOException("FTP server refused connection: " + ftpClient.getReplyString());
}
if (!ftpClient.login(username, password)) {
throw new IOException("FTP login failed: " + ftpClient.getReplyString());
}
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
// 检查文件是否存在
if (!ftpClient.exists(remoteFilePath)) {
throw new IOException("文件不存在: " + remoteFilePath);
}
// 获取文件大小
long fileSize = ftpClient.size(remoteFilePath);
if (fileSize < 0) {
// 如果SIZE命令不支持,回退到listFiles方法
FTPFile[] files = ftpClient.listFiles(remoteFilePath);
if (files == null || files.length == 0) {
throw new IOException("无法获取文件信息: " + remoteFilePath);
}
fileSize = files[0].getSize();
}
return fileSize;
} finally {
if (ftpClient.isConnected()) {
try {
ftpClient.logout();
} catch (IOException e) {
// 忽略登出时的异常
}
ftpClient.disconnect();
}
}
}
代码提供了多种获取FTP文件大小的方法,你可以根据实际需求选择最适合的方案。


