请写出ftpClient获取远程文件大小的代码示例
时间: 2024-06-10 19:08:19 浏览: 10
好的,下面是一个 Java 示例代码:
```
import org.apache.commons.net.ftp.FTPClient;
public class FtpClientDemo {
public static void main(String[] args) {
String server = "ftp.example.com";
int port = 21;
String user = "username";
String pass = "password";
String remoteFilePath = "/path/to/remote/file.txt";
FTPClient ftp = new FTPClient();
try {
ftp.connect(server, port);
ftp.login(user, pass);
long size = ftp.getSize(remoteFilePath);
System.out.println("Remote file size: " + size);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
ftp.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
```
这个示例使用了 Apache Commons Net 库提供的 FTPClient 类,通过连接服务器、登录、调用 getSize() 方法获取远程文件的大小。如果 FTP 服务器允许获取文件大小,这段代码应该能够正常运行。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)