我編寫了這個類(從示例)下載包含在遠程FTP文件夾中的所有文件的標題。它工作的很好,但是當它接近下載文件#146時,它會停止並出現NullPointException。文件#146存在,我可以將其作爲單個文件實際下載。FTP下載停止在文件#146
在該方法中,remotePathLong包含寫在一行中的所有遠程文件夾並以空格字符隔開。
public void downloadHeader(String remotePathLong, String destPath, int bytes) {
String remotePath;
FTPFile[] fileList;
String[] fileNameList;
FTPClient ftpClient = new FTPClient();
try {
ftpClient.connect(server);
ftpClient.login(user, pass);
ftpClient.enterLocalPassiveMode();
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
int indice = 0;
int iP = 1;
File downloadFile;
String destFile;
String remoteFile;
byte[] bytesArray;
int bytesRead = -1;
while ((remotePath = getPath(remotePathLong, iP)) != null) {
System.out.println("Loading file list from the server.....");
fileNameList = ftpClient.listNames(remotePath);
for (String file : fileNameList) {
indice += 1;
System.out.println(indice + " - Downloading: " + file);
//Select files
destFile = destPath.concat(file);
downloadFile = new File(destFile);
outputStream = new BufferedOutputStream(new FileOutputStream(downloadFile));
//Download remote file (from ftp)
remoteFile = remotePath.concat(file);
inputStream = ftpClient.retrieveFileStream(remoteFile);
bytesArray = new byte[bytes];
bytesRead = inputStream.read(bytesArray);
outputStream.write(bytesArray);
//Save into file
outputStream.close();
inputStream.close();
iP += 1;
}
}
} catch (IOException ex) {
} final{
try {
if (ftpClient.isConnected()) {
ftpClient.logout();
ftpClient.disconnect();
}
}
catch (IOException ex1) {
System.out.println("Error: " + ex1.getMessage());
}
}
當它到達bytesRead = inputStream.read(bytesArray)時,在迭代#146它給出錯誤。但如果在同一次迭代中,我重新初始化它的連接。 請問有人有建議嗎?
在迭代#146重新初始化連接? (這裏只是半開玩笑) –
請鏈接到示例? – hd1
如果問題一致,則檢查文件權限 –