public static void main(String[] args) {
Map map = new HashMap();
map.put("ip", "88888");
map.put("port", 21);
map.put("username", "888888");
map.put("password", "8888888");
map.put("dir","test");
try {
FTPClient ftpClient = createFTPClient(map);
List<FTPFile> ftpFiles = listFiles(ftpClient,null);
for (FTPFile ftpFile:ftpFiles){
System.out.println(ftpFile.getName());
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);//文件下载要使用二进制模式
File localFile = new File("D:"+ "/test" + ftpFile.getName());
OutputStream outputStream = new FileOutputStream(localFile);
Boolean b=ftpClient.retrieveFile(ftpFile.getName(),outputStream);
System.out.println(b);
outputStream.close();
}
ftpClient.logout();
} catch (Exception e) {
e.printStackTrace();
}
}
public static FTPClient createFTPClient(Map cfg) throws Exception {
if (!cfg.containsKey("ip"))
throw new Exception("缺少ip配置");
else if (!cfg.containsKey("username"))
throw new Exception("缺少username配置");
else if (!cfg.containsKey("password"))
throw new Exception("缺少password配置");
FtpCfg ftpCfg = new FtpCfg();
ftpCfg.setIp(cfg.get("ip").toString());
ftpCfg.setPort(cfg.get("port") == null ? null : Integer.valueOf(cfg.get("port").toString()));
ftpCfg.setUsername(cfg.get("username").toString());
ftpCfg.setPassword(cfg.get("password").toString());
ftpCfg.setWorkingDir(cfg.get("dir") == null ? null : cfg.get("dir").toString());
return createFTPClient(ftpCfg);
}
ftp下载文件到本地
最新推荐文章于 2021-02-13 00:41:31 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)