Android FTPClient - retrieveFileStream() always returns null

一曲冷凌霜 提交于 2019-12-06 02:37:17

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()

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.

          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;
    }

}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!