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;
}