baihaojie

public static String downFile(String ip,String ftpFileName,String savePath,String fileName) {
FTPClient ftp =new FTPClient();
try{
int reply;
ftp.connect(ip,8010);
ftp.login("anonymous", null);//登录(匿名用户登录)
reply = ftp.getReplyCode();
if(!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return null;
}
ftp.changeWorkingDirectory(ftpFileName);//转移到FTP服务器目录
FTPFile[] fs = ftp.listFiles();
for(FTPFile ff:fs){
if(ff.getName().equals(fileName)){
File localFile =new File(savePath + ff.getName());
if(!localFile.exists()){
if (!localFile.getParentFile().exists()) {
localFile.getParentFile().mkdirs();
}
localFile.createNewFile();
}
OutputStream is =new FileOutputStream(localFile);
ftp.setFileType(ftp.BINARY_FILE_TYPE);
ftp.retrieveFile(ff.getName(),is);
is.close();
}
}
ftp.logout();
}catch(Exception e){
e.printStackTrace();
}finally{
if(ftp.isConnected()) {
try{

ftp.disconnect();

}catch(IOException ioe) {

}
}
}
return fileName;
}

分类:

技术点:

相关文章:

  • 2022-12-23
  • 2021-11-24
  • 2022-12-23
  • 2021-07-20
  • 2021-09-09
  • 2021-10-07
  • 2021-09-19
猜你喜欢
  • 2021-10-30
  • 2021-10-24
  • 2021-11-25
  • 2020-12-21
  • 2021-04-05
  • 2021-08-13
  • 2021-12-15
相关资源
相似解决方案