FTPClient实现
commons-net库
库官网
支持多协议
下载中文路径下的文件乱码问题
重点: ///重点在于这句一定要放在连接登录之前 ftpClient.setControlEncoding("gbk")
go
public class FTPDownLoadExample {
public static void main(String[] args) {
FTPClient ftpClient = new FTPClient();
try {
///重点在于这句一定要放在连接登录之前
ftpClient.setControlEncoding("gbk");
ftpClient.connect("host", 21);
ftpClient.login("javabobby", "javabobby");
ftpClient.enterLocalPassiveMode();
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
String remoteFile = "/中文/xxx.pdf";
String localFile = "D:/xxxx.pdf";
boolean exists = ftpClient.changeWorkingDirectory("/中文");
if (!exists) {
System.out.println("目录不存在");
return;
}
try (OutputStream outputStream = new FileOutputStream(localFile)) {
boolean success = ftpClient.retrieveFile(remoteFile, outputStream);
if (success) {
System.out.println("文件下载成功");
} else {
System.out.println("文件下载失败");
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (ftpClient.isConnected()) {
ftpClient.logout();
ftpClient.disconnect();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
往期推荐
<>
Windows服务器重启了,Java服务可以自动重启?
<>
<>
<>
排序算法
<>
<>
数据结构与算法基础篇
<>