2013-10-13 128 views
-1

我試圖從FTP順序下載兩個文件。首先下載文件沒有任何問題。但是當InputStreamftpClient.retrieveFileStream()初始化時,第二次拋出NullPointerException。我試圖改變序列,但它沒有效果。文件存在並在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(); 
     } 
    } 

logcat的:

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並在方法內部打開/關閉連接。

+0

哪一行是'ActivityMain.java:170'? – ssantos

+0

@ssantos - 他在源文件中註明了這一點 - 尋找評論 – kamituel

+0

對,我的壞... – ssantos

回答

0

我認爲這個問題是這行代碼:

final String PATH = getExternalCacheDir().toString() + "/"; 

,因爲該方法getExternalCacheDir(),並僅返回你的緩存目錄,如果緩存不是空。如果沒有緩存,則不會有目錄。

您可以改爲使用getExternalFilesDir()

參考:

編輯:請修改代碼到初始化ftpClient到:

int reply; 
ftp.connect(server,port); 
reply = ftp.getReplyCode(); 

,並檢查所述回覆碼。

+0

使用getExternalFilesDir()沒有效果。在同一地方同樣例外。 –

+0

你確定FTP hsd不止兩個文件..? – user2339071

+0

FTP有很多文件,但我想直接加載2個文件。不再。它們存在於服務器上。但第一次加載後 - 秒拋出異常。 –

0

我認爲你的解決方案是掩蓋你的問題,這是你沒有在關閉你的流之前在downloadFile()中調用ftpClient.completePendingCommand()。

相關問題