Android的FtpClient的 - retrieveFileStream()始终返回null [英] Android FTPClient - retrieveFileStream() always returns null

查看:2889
本文介绍了Android的FtpClient的 - retrieveFileStream()始终返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新手到Android。我想下载FTP服务器上的文件使用Apache共享FtpClient的到SD卡。该行的InputStream输入= client.retrieveFileStream(/+文件名); 总是返回null。但该文件是存在的FTP位置。请帮助我知道该错误是。

I am a newbie to Android. I am trying download a file from ftp server to sdcard using Apache Commons FTPClient. The line InputStream input = client.retrieveFileStream("/" + fileName); always returns null. But the file is there in Ftp location. Kindly help me to know where the mistake is.

我已经设置在我的清单中的下列权限;机器人:名称=android.permission.INTERNET对和android:NAME =android.permission.WRITE_EXTERNAL_STORAG​​E

I have set the following permissions in my manifest; android:name="android.permission.INTERNET" and android:name="android.permission.WRITE_EXTERNAL_STORAGE"

我的code

My Code

private static void downLoad(){
    FTPClient client = new FTPClient();
    FileOutputStream fos = null;

    try {
        client.connect("ftp.doamin.com");
        client.login("8888", "8888");
String filePath = "/mnt/sdcard/download/CheckboxHTML.txt" ;
String fileName = "CheckboxHTML.txt";
fos = new FileOutputStream(filePath);
InputStream input = client.retrieveFileStream("/" + fileName);
byte[] data = new byte[1024];
int count  = input.read(data); 
while ((count = input.read(data)) != -1) {
      fos.write(data, 0, count);
}
fos.close();
      if(!client.completePendingCommand()) { 
      client.logout(); 
      client.disconnect(); 
      System.err.println("File transfer failed."); 
} 
    } catch (SocketException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IOException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
    }

}

感谢您的时间和兴趣。 Ananth。

Thanks for your time and interest. Ananth.

推荐答案

AFAIK。你是想通过调用completePendingCommand(以完成文件传输)并确认转让确实是成功的。即你需要添加调用下面fos.clos()的函数。

AFAIK. You are suppose to finalize file transfers by calling completePendingCommand() and verifying that the transfer was indeed successful. i.e. you need to add call the function below fos.clos().

fos.close();
client.completePendingCommand()

您也可以考虑这一点,根据对FTPClient.retrieveFileStream()的API,该方法返回null时,它无法打开数据连接,在这种情况下,你应该检查的答复code(例如的getreply code(),getReplyString(),getReplyStrings()),看看它为什么失败。

You may also consider this, according to the API for FTPClient.retrieveFileStream(), the method returns null when it cannot open the data connection, in which case you should check the reply code (e.g. getReplyCode(), getReplyString(), getReplyStrings()) to see why it failed.

这篇关于Android的FtpClient的 - retrieveFileStream()始终返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆