下面代码正常下载。
public boolean downFileFromFTP(String url, int port, String username,
String password, String remotePath, String fileName,
String localPath) {
boolean success = false;
FTPClient ftp = new FTPClient();
logger.info( "url="+url );
logger.info( "port="+port );
logger.info( "username="+username );
logger.info( "password="+password );
logger.info( "remotePath="+remotePath );
logger.info( "localPath="+localPath );
logger.info( "fileName="+fileName );
try {
ftp.connect(url, port);
logger.info( "连接FTP服务器成功!" );
// ftp.setControlEncoding(“GBK”);
ftp.login(username, password);
ftp.setFileType(FTP.BINARY_FILE_TYPE);
logger.info( “登录FTP服务器成功!” );
//设置linux环境
FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_UNIX);
ftp.configure(conf);
//判断是否连接成功
int reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
System.out.println("***reply["+reply+"]");
ftp.disconnect();
return success;
}
ftp.changeWorkingDirectory(remotePath);
logger.info( "进入远程目录1:"+ remotePath );
// 设置被动模式
ftp.setRemoteVerificationEnabled(false);
ftp.enterLocalPassiveMode();
FTPFile[] fs = ftp.listFiles();
logger.info( "远程目录文件列表:"+ fs.toString() );
logger.info("v9.91");
for (FTPFile ff : fs) {
if (!ff.getName().equals(fileName))
continue;
File localFile = new File(localPath + "/" + ff.getName());
OutputStream is = new FileOutputStream(localFile);
ftp.retrieveFile(ff.getName(), is);
// ftp.retrieveFile(new String(ff.getName().getBytes(“GBK”),“ISO-8859-1”), is);
is.close();
}
ftp.logout();
success = true;
} catch (IOException e) {
//System.out.println("error2" + e.getMessage());
logger.info( "error:" + e.getMessage() );
e.printStackTrace();
if (ftp.isConnected())
try {
ftp.disconnect();
} catch (IOException localIOException2) {
}
} finally {
if (ftp.isConnected())
try {
ftp.disconnect();
} catch (IOException localIOException3) {
}
}
return success;
}