首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Android FTPClient - retrieveFileStream()总是返回null

Android FTPClient - retrieveFileStream()总是返回null
EN

Stack Overflow用户
提问于 2011-11-24 13:12:48
回答 3查看 10.7K关注 0票数 3

我是Android的新手。我正在尝试使用从ftp服务器下载一个文件到sdcard。InputStream input = client.retrieveFileStream("/“+fileName)行;总是返回null。但是文件在Ftp的位置。请帮助我知道错误在哪里。

我在清单中设置了以下权限: android:name="android.permission.INTERNET“和

我的代码

代码语言:javascript
运行
复制
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();
    }

}

谢谢你的时间和兴趣。安娜思。

EN

回答 3

Stack Overflow用户

发布于 2013-06-05 12:20:34

AFAIK。您应该通过调用completePendingCommand()并验证传输确实成功来完成文件传输。也就是说,您需要在fos.clos()下面添加调用函数。

代码语言:javascript
运行
复制
fos.close();
client.completePendingCommand()

您还可以考虑这一点,根据FTPClient.retrieveFileStream()的API,方法在无法打开数据连接时返回null,在这种情况下,您应该检查应答代码(例如,getReplyCode()、getReplyString()、getReplyStrings())以查看失败的原因。

票数 7
EN

Stack Overflow用户

发布于 2012-11-09 10:03:46

代码语言:javascript
运行
复制
          File file = new File(Environment.getExternalStorageDirectory() + "/pdf");
        if(!file.exists())

           file.mkdir(); //directory is created;
            try {
                ftp = new FTPClient();
                ftp.connect("yours ftp URL",21);//don't write ftp://

                try {
                    int reply = ftp.getReplyCode();
                  if (!FTPReply.isPositiveCompletion(reply)) {
                    throw new Exception("Connect failed: " + ftp.getReplyString());
                    }
                    if (!ftp.login("username","password")) {
                      throw new Exception("Login failed: " + ftp.getReplyString());
                    }
                    try {
                        ftp.enterLocalPassiveMode();
                        if (!ftp.setFileType(FTP.BINARY_FILE_TYPE)) {
//                          Log.e(TAG, "Setting binary file type failed.");
                        }

                        transferFile(ftp);
                    } catch(Exception e) {
//                      handleThrowable(e);
                    } finally {
                        if (!ftp.logout()) {
//                          Log.e(TAG, "Logout failed.");
                        }
                    }
                } catch(Exception e) {
//                  handleThrowable(e);
                } finally {
                    ftp.disconnect();
                }
            } catch(Exception e) {
//              handleThrowable(e);
            }


    }


       private void transferFile(FTPClient ftp) throws Exception {
        long fileSize=0;
        fileSize = getFileSize(ftp, "nag.pdf");
        Log.v("async","fileSize"+fileSize);
        if(!(fileSize==0)){
            InputStream is = retrieveFileStream(ftp,  "nag.pdf");
            downloadFile(is,  fileSize);
            is.close();
            }
            else


                 //nosuch files

        if (!ftp.completePendingCommand()) {
            throw new Exception("Pending command failed: " + ftp.getReplyString());
        }
    }

    private InputStream retrieveFileStream(FTPClient ftp, String filePath)
    throws Exception {
        InputStream is = ftp.retrieveFileStream(filePath);
        int reply = ftp.getReplyCode();
        if (is == null
                || (!FTPReply.isPositivePreliminary(reply)
                        && !FTPReply.isPositiveCompletion(reply))) {
            throw new Exception(ftp.getReplyString());
        }
        return is;
    }

    private byte[] downloadFile(InputStream is, long fileSize)
    throws Exception {
    outputStream os = newFileOutputStream(Environment.getExternalStorageDirectory()
                + "/pdf/nag.pdf");  
        byte[] buffer = new byte[(int) fileSize];
        int readCount;
        while( (readCount = is.read(buffer)) > 0) {
            os.write(buffer, 0, readCount);
        }
        Log.i("tag", "buffer = " + buffer);
        return buffer; // <-- Here is your file's contents !!!
    }

    private long getFileSize(FTPClient ftp, String filePath) throws Exception {
        long fileSize = 0;
        FTPFile[] files = ftp.listFiles(filePath);
        if (files.length == 1 && files[0].isFile()) {
            fileSize = files[0].getSize();
        }
        Log.i("tag", "File size = " + fileSize);
        return fileSize;
    }

}
票数 0
EN

Stack Overflow用户

发布于 2020-03-04 12:49:35

在研究了这个问题并花了几个小时之后,我发现AndroidApacheretrieveFile()retrieveFileStream有时在FileSize太大的时候不能很好地工作。

确保将正确的TypeFile设置为BinaryFile

mFTPClient.setFileType(FTP.BINARY_FILE_TYPE);

也不要忘记传入LocalPassiveMode下载命令。

mFTPClient.enterLocalPassiveMode();

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8257639

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档