Ananth
Ananth

Reputation: 31

Android FTPClient - retrieveFileStream() always returns null

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.

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

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

}

Thanks for your time and interest. Ananth.

Upvotes: 3

Views: 11152

Answers (3)

VelocityPulse
VelocityPulse

Reputation: 632

After have worked on this problem and spent hours, I've found out that the Android Apache retrieveFile() and retrieveFileStream sometimes don't work very well when the FileSize is too big.

Ensure to set the right TypeFile to BinaryFile

mFTPClient.setFileType(FTP.BINARY_FILE_TYPE);

Also never forget to pass in LocalPassiveMode for download commands

mFTPClient.enterLocalPassiveMode();

Upvotes: 0

M. Usman Khan
M. Usman Khan

Reputation: 4408

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.

Upvotes: 7

gaddam nagaraju
gaddam nagaraju

Reputation: 187

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

}

Upvotes: 0

Related Questions