【发布时间】:2014-08-27 15:24:40
【问题描述】:
我需要将图像从 FTP 服务器下载到 Android 设备。我使用 ftp4j-1.7.2.jar 库尝试了几个示例,但无法连接 FTP 服务器并且搞砸了。
有人使用过 FTP 服务器吗?
请建议从服务器建立连接和下载文件。
【问题讨论】:
我需要将图像从 FTP 服务器下载到 Android 设备。我使用 ftp4j-1.7.2.jar 库尝试了几个示例,但无法连接 FTP 服务器并且搞砸了。
有人使用过 FTP 服务器吗?
请建议从服务器建立连接和下载文件。
【问题讨论】:
使用库commons.apache.org/proper/commons-net
检查以下代码以从 FTP 服务器下载文件:
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();
}
}
}
【讨论】: