How to download a file from FTP server to Android device?

后端 未结 1 639
旧巷少年郎
旧巷少年郎 2020-12-29 13:53

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,

相关标签:
1条回答
  • 2020-12-29 14:24

    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();
        }
    }
    }
    
    0 讨论(0)
提交回复
热议问题