我编写了这个类(从示例)下载包含在远程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
如果问题一致,则检查文件权限 –