8

I have a requirement to download a image from FTP server to android device. I tried with few samples by using ftp4j-1.7.2.jar library, but failed to connect with FTP servers & messed up.

Have anyone worked with FTP servers?

Please suggest to make connection & download file from Server.

The Heist
  • 1,444
  • 1
  • 16
  • 32
Anil kumar
  • 1,534
  • 3
  • 14
  • 20

1 Answers1

13

Use library commons.apache.org/proper/commons-net

Check below code to download file from FTP server:

private Boolean downloadAndSaveFile(String server, int portNumber,
    String user, String password, String filename, File localFile)
    throws IOException {
FTPClient ftp = null;

try {
    ftp = new FTPClient();
    ftp.connect(server, portNumber);
    Log.d(LOG_TAG, "Connected. Reply: " + ftp.getReplyString());

    ftp.login(user, password);
    Log.d(LOG_TAG, "Logged in");
    ftp.setFileType(FTP.BINARY_FILE_TYPE);
    Log.d(LOG_TAG, "Downloading");
    ftp.enterLocalPassiveMode();

    OutputStream outputStream = null;
    boolean success = false;
    try {
        outputStream = new BufferedOutputStream(new FileOutputStream(
                localFile));
        success = ftp.retrieveFile(filename, outputStream);
    } finally {
        if (outputStream != null) {
            outputStream.close();
        }
    }

    return success;
} finally {
    if (ftp != null) {
        ftp.logout();
        ftp.disconnect();
    }
}
}
Anil Jadhav
  • 2,128
  • 1
  • 17
  • 31
  • Hi, I Installed FileZilla FTP server in my desktop. Its address is 127.0.0.1, Port is 14147. What I have to give for UserName, Password, fileName, localfile?? – Anil kumar Jul 07 '14 at 08:07
  • Use this link http://lifehacker.com/339887/build-a-home-ftp-server-with-filezilla to create a user with filezilla & to know that how to use it. OR check this for same setups http://www.addictivetips.com/windows-tips/how-to-setup-personal-ftp-server-using-filezilla-step-by-step-guide/ – Anil Jadhav Jul 07 '14 at 08:11
  • What is the Library you are using??? I am geting java.lang.NullPointerException at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:471) – Anil kumar Jul 07 '14 at 09:29