我写了一个简单的java应用程序,它将使用Apache Commons Net库从FTP服务器下载文件,服务器位于Android手机上。我尝试了几个设置和几个FTPServer应用程序,但我无法让它工作。该应用程序刚开始下载后挂起。到目前为止,我找到的唯一解决方法是设置数据超时,然后捕获异常并使用新连接创建一个新的客户端实例以下载下一个文件。有谁知道怎么做对吗?
FileOutputStream outputStream = new FileOutputStream(local);
boolean downloaded = false;
try {
//InputStream retrieveFileStream = getFtpClient().retrieveFileStream(remoteFilePath);
//new BufferedOutputStream(outputStream);
FTPClient c = new FTPClient();
c= new FTPClient();
c.connect(host, port);
c.enterLocalPassiveMode();
c.setSoTimeout(1000 * 20);
c.setDefaultTimeout(1000 * 20);
c.login(user, pass);
c.setBufferSize(1024 * 8);
c.setFileType(FTP.BINARY_FILE_TYPE);
c.setDataTimeout(20 * 1000);
downloaded = c.retrieveFile(remoteFilePath, outputStream);
if (!downloaded) {
String[] replyStrings = c.getReplyStrings();
LOG.info("not downloaded code {} Messages {}", c.getReplyCode(), Arrays.toString(replyStrings));
local.delete();
} else {
LOG.info("file was downloaded");
}
} catch (IOException iOException) {
if (!(iOException.getCause() instanceof SocketTimeoutException)) {
LOG.info("Caught iOException do shut down" + iOException.getMessage());
logoutAndDisconnect();
} else {
LOG.info("Caught SocketTimeoutException assuming file was downloaded " + iOException.getMessage());
downloaded = true;
}
} catch (Exception e) {
LOG.error("ERROR: ", e.getMessage(), e);
logoutAndDisconnect();
} finally {
outputStream.close();
}
return downloaded;
}
此处描述了类似的问题http://www.egeek.me/2012/11/22/downloading-a-file-via-ftp-in-android/,但我是本地网络
答案 0 :(得分:0)
手机上的DroidWall阻止了连接。通常禁用它或允许内核连接可以解决问题。