我試圖從服務器下載一個文件,但它得到0字節......FTPClient下載一個0字節的文件
這是我FTPDownload類
public boolean getFile(String filename){
try {
FTPClient ftpClient = new FTPClient();
ftpClient.connect(ftpAddress, ftpPort);
ftpClient.login(ftpUser, ftpPass);
int reply = ftpClient.getReplyCode();
//FTPReply stores a set of constants for FTP reply codes.
if (!FTPReply.isPositiveCompletion(reply))
{
ftpClient.disconnect();
return false;
}
ftpClient.enterLocalPassiveMode();
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
ftpClient.setBufferSize(1024*1024);
String remoteFile = serverPath + filename;
logger.debug("remote file is: "+remoteFile); //correct path
File tempFile = new File(downloadDir+"temp.jar");
logger.debug("file will be "+tempFile.toString()); //correctly created
OutputStream os = new BufferedOutputStream(new FileOutputStream(tempFile));
ftpClient.retrieveFile(remoteFile, os);
os.close();
String completeJarName = downloadDir+jarName;
//delete previous file
File oldFile = new File(completeJarName);
FileUtils.forceDelete(oldFile);
//rename
File newFile = new File(completeJarName);
FileUtils.moveFile(tempFile, newFile);
if (ftpClient.isConnected()) {
ftpClient.logout();
ftpClient.disconnect();
}
} catch (IOException e) {
// TODO Auto-generated catch block
logger.error("errore ftp", e);
return false;
}
return true;
}
基本上,臨時呸得到創建,然後前面的文件被取消,臨時文件被重命名,但它是0字節...我不明白哪裏出了問題...
我不確定,但嘗試在'ftpClient.retrieveFile'之後但在'os.close()'之前刷新輸出流'os.flush()'。 – Shadov