FTP에서 두 파일을 순차적으로 다운로드하려고합니다. 아무 문제없이 첫 번째 파일 다운로드. 그러나 InputStream
이 ftpClient.retrieveFileStream()
으로 초기화되면 NullPointerException이 throw됩니다. 시퀀스를 변경하려고했지만 효과가 없습니다. 파일이 존재하고 FTP에서 사용할 수 있습니다.FTP에서 파일을 다운로드하려고하면 NullPointerException이 발생합니다.
CODE
FTPClient ftpClient;
@Override
protected void onPreExecute() {
String server = "xxxxxxxxxxxxxxxxxxxxx";
int port = 21;
String user = "xxxxxx";
String pass = "xxxxxx";
long fileSize = 0;
try {
ftpClient = new FTPClient();
ftpClient.connect(server, port);
ftpClient.login(user, pass);
ftpClient.enterLocalPassiveMode();
fileSize += getFileSize(ftpClient, CARDS_DB);
fileSize += getFileSize(ftpClient, IMG_DB);
showDialog(PROGRESS_DLG_ID);
downloadProgress.setMax((int) fileSize);
} catch (Exception e) {
Log.e("ERROR", e.toString());
}
}
@Override
protected Boolean doInBackground(String... strings) {
final String PATH = getExternalCacheDir().toString() + "/";
int total = 0;
try {
downloadFile(ftpClient, PATH, CARDS_DB, total);
downloadFile(ftpClient, PATH, IMG_DB, total);
} catch (Exception e) {
Log.e("EXCEPTION", e.toString());
return false;
} finally {
try {
if (ftpClient.isConnected()) {
ftpClient.logout();
ftpClient.disconnect();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
return true;
}
private void downloadFile(FTPClient ftpClient, String PATH, String fileName, int total) {
try {
File downloadFile = new File(PATH + fileName);
OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(downloadFile));
InputStream inputStream;
inputStream = ftpClient.retrieveFileStream(fileName); //here i have exception at second method invocation.
byte[] bytesArray = new byte[8192];
int bytesRead;
while ((bytesRead = inputStream.read(bytesArray)) != -1) {
total += bytesRead;
outputStream.write(bytesArray, 0, bytesRead);
publishProgress(total);
}
outputStream.close();
inputStream.close();
} catch (Exception ex) {
Log.d("ERROR", "Error: " + ex.getMessage());
ex.printStackTrace();
}
}
로그 캣 :
10-13 14:51:29.519 W/System.err(9554): java.lang.NullPointerException
10-13 14:51:29.529 W/System.err(9554): at com.FHS.ActivityMain$DBLoad.downloadFile(ActivityMain.java:170)
10-13 14:51:29.529 W/System.err(9554): at com.FHS.ActivityMain$DBLoad.doInBackground(ActivityMain.java:141)
10-13 14:51:29.529 W/System.err(9554): at com.FHS.ActivityMain$DBLoad.doInBackground(ActivityMain.java:102)
10-13 14:51:29.529 W/System.err(9554): at android.os.AsyncTask$2.call(AsyncTask.java:185)
10-13 14:51:29.529 W/System.err(9554): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
10-13 14:51:29.529 W/System.err(9554): at java.util.concurrent.FutureTask.run(FutureTask.java:138)
10-13 14:51:29.529 W/System.err(9554): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
10-13 14:51:29.529 W/System.err(9554): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
10-13 14:51:29.529 W/System.err(9554): at java.lang.Thread.run(Thread.java:1019)
해결 문제 해결. 메소드 downloadFile()을 호출 할 때마다 메소드 내에서 FTPClient를 초기화하고 열기/닫기 연결이 필요합니다.
어떤 줄이'ActivityMain.java : 170'입니까? – ssantos
@ssantos - 그는 이것을 소스에서 언급했습니다 - 코멘트를 찾으십시오 – kamituel
맞아요, 제 잘못 ... – ssantos