2014-07-04 72 views
1

我想要讀取目錄中的文件。我想添加: List<String> nomi = new ArrayList<String>(); 文件Nomi.txt的線串。從FTP服務器讀取文件,但InputStream始終爲空

通過調試我正確地查看鏈接(001.jpg 002.jpg 003.jpg)和ft(Nomi.txt)中的文件,但在流中我始終爲空;

InputStream stream = f.retrieveFileStream(/*url_ftp + "/photo/"+ft*/ft); 

我完整的代碼是這樣的:

private static abstract class GetLinksTask extends AsyncTask<String, Void, List<String>> { 

    protected List<String> doInBackground(String... urls) { 
     List<String> links = new ArrayList<String>(); 
     String ft=null; 
     BufferedReader reader = null; 
     List<String> nomi = new ArrayList<String>(); 
     FTPClient f = new FTPClient(); 

     try { 
      int reply; 
      f.connect(url_ftp); 
      f.login(username,password); 
      reply = f.getReplyCode(); 
      if (!FTPReply.isPositiveCompletion(reply)) { 
       f.disconnect(); 
       System.err.println("FTP server refused connection."); 
      } 
      FTPListParseEngine engine = f.initiateListParsing("photo"); 

      while (engine.hasNext()) { 
       FTPFile[] files = engine.getNext(25); // "page size" you want 
       //FTPFile[] files = engine.getFiles(filter); 
       for (FTPFile file : files) { 
        if(file.getName().substring(file.getName().length()-3,file.getName().length()).equals("jpg")){ 
         System.out.println(file.getName()); 
         links.add(file.getName()); 
        }else{ 
         ft=file.getName(); 
         InputStream stream = f.retrieveFileStream(/*url_ftp + "/photo/"+ft*/ft); 
         reader = new BufferedReader(new InputStreamReader(stream, "UTF-8")); 
         //nomi.add(reader.readLine());         
        } 
        System.out.println(file.getName()); 
       } 
       //names=nomi; 
      } 
     }catch (IOException e) { 
      e.printStackTrace(); 
     }finally { 
      if (reader != null) 
       try { 
        reader.close(); 
       }catch (IOException logOrIgnore) { 
      } 
     } 

     return links; 
    } 

    protected abstract void postExecute(List<String> links); 

    protected void onPostExecute(List<String> lists) { 
     postExecute(lists); 
    } 

} 

一些建議嗎? 感謝

+0

第一件事第一件事。檢查鏈接是否有效。並且您可以使用其他ftp客戶端(例如telnet)連接到它 – nafas

+0

yes是全部正確我使用FILE ZILLA – user3589887

+0

什麼是完全無效的呢?你可以連接到它嗎?重播的價值是什麼? – nafas

回答

1

這是不夠的,建立一個讀者

reader = new BufferedReader(new InputStreamReader(stream, "UTF-8")); 

,並關閉它:

reader.close(); 

某處,在兩者之間,您實際上已經讀取數據:

String line; 
while((line = reader.readLine()) != null){ 
    nomi.add(line); 
} 
+0

,但我有stream = null,如果我運行我的應用程序,我有崩潰!!!在哪裏我必須添加此代碼? – user3589887

+0

這裏:在reader.close()之前; – laune

+0

不是這個問題,爲什麼當我運行這個:InputStream stream = f.retrieveFileStream(/ * url_ftp +「/ photo /」+ ft */ft);我查看stream = null,當我運行下一條指令時,我有「崩潰應用程序」:reader = new BufferedReader(new InputStreamReader(stream,「UTF-8」)); - user3589887 9分鐘前 – user3589887

0

我解釋了從FTP服務器獲取inputStream的完整代碼,然後如何從該輸入流讀取數據。我假設你正在使用TLS/SSL安全層。

public FTPSClient makeFTPConnection(String vserver, int vport, String vuser, String vpassword) { 
    LOGGER.debug("ENTRY"); 
     try { 
     ftpClient = new FTPSClient(); 
    } catch (NoSuchAlgorithmException e1) { 
     // TODO Auto-generated catch block 
     e1.printStackTrace(); 
    } 
    try { 

     ftpClient.connect(vserver, vport); 
     ftpClient.login(vuser, vpassword); 

     ftpClient.execPBSZ(0); 
     ftpClient.execPROT("P"); 
     ftpClient.changeWorkingDirectory("/"); 
     ftpClient.changeWorkingDirectory("/feeds"); 
     ftpClient.enterLocalPassiveMode(); 
     ftpClient.setFileType(FTP.BINARY_FILE_TYPE); 

    } catch (Exception e) { 
     LOGGER.error(e.getMessage()); 
    } 
    /* // int reply=ftpClient.getReply(); 
     String replyStr=ftpClient.getReplyString(); 
     ftpClient.getAuthValue();*/ 

    LOGGER.debug("EXIT"); 
    return ftpClient; 
} 

然後進行連接後,我們會檢查天氣文件存在於ftp還是不存在。

public InputStream checkWOFileExistAtFTP(FTPSClient ftpClient, String host,String user, String filePath) throws IOException { 

    int returnCode; 
    // filePath="ftp://"+user+"@"+host+"/"+filePath; 
    InputStream inputStream = ftpClient.retrieveFileStream(filePath); 

    String dd=ftpClient.getReplyString(); 
    returnCode = ftpClient.getReplyCode(); 
    if (inputStream == null || returnCode == 550) { 
     return null; 
    } 
    return inputStream; 

} 

現在我們已經在上面的方法中獲得了inputStream現在從它讀取數據的時間。

  BufferedReader br = new BufferedReader(new InputStreamReader(inputStream)); 

      System.out.println("Reading file start."); 

      char[] charBuffer = new char[8 * 1024]; 
      StringBuilder builder = new StringBuilder(); 
      int numCharsRead; 
      while ((numCharsRead = br.read(charBuffer, 0, charBuffer.length)) != -1) { 
       builder.append(charBuffer, 0, numCharsRead); 
      } 
//will print all data 
system.out.println(builder.toString());