java中用ftp下载服务器上的文件,下载是自己电脑的路径,在本地跑,下载没有问题,真实环境就不行了,真实环境根本找不到路径?
如何解决啊~~~在线等~~~~~
public static File downloadFile(
String url, //FTP服务器hostname
int port,//FTP服务器端口
String username, //FTP登录账号
String password, //FTP登录密码
String path,//FTP服务器上的相对路径
String fileName,//要下载的文件名
String localPath,//下载后保存到本地的路径
String name//保存文件名
) {
boolean success = false;
FTPClient ftp = new FTPClient();
int reply;
try {
ftp.connect(url, port);
ftp.setDefaultPort(port);
ftp.setControlEncoding("UTF-8");
ftp.connect(url, port);// 连接FTP服务器
ftp.login(username, password);// 登录
ftp.setFileType(FTP.BINARY_FILE_TYPE);
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return null;
}
isDirExist(ftp, "/"+path);
ftp.changeWorkingDirectory(path);
ftp.enterLocalPassiveMode();
FTPFile[] fs = ftp.listFiles();
File localFile=null;
for(FTPFile ff:fs){
if(ff.getName().equals(fileName)){
localFile= new File(localPath+"/"+name);
OutputStream is = new FileOutputStream(localFile);
ftp.retrieveFile(ff.getName(), is);
is.close();
}
}
ftp.logout();
return localFile;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
}
}
}
return null;
}