从FTP服务器读取文件但InputStream始终为null

时间:2014-07-04 09:56:01

标签: java android eclipse

我想在目录中读取文件。我想加入:     List<String> nomi = new ArrayList<String>(); 文件Nomi.txt的线串。

使用debug我正确查看链接(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);
    }

}

一些提示? 感谢

2 个答案:

答案 0 :(得分:1)

仅创建一个Reader

是不够的
reader = new BufferedReader(new InputStreamReader(stream, "UTF-8"));

并关闭它:

reader.close();

在某处,介于两者之间,你实际上必须阅读数据:

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

答案 1 :(得分: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());