java - Android 如果使用 FTP 协议断开互联网连接,如何从离开的位置自动重试下载
问题描述
下面是在android中使用FTP下载文件的代码。它工作正常,但万一下载期间互联网中断,它不会重试。我希望它应该在互联网恢复时自动重试,并且应该从它离开的地方开始下载。请帮我。
public static void downloadFile(Context context) {
FTPClient client = new FTPClient();
FileOutputStream fos = null;
try {
client.connect("test.rebex.net");
client.login("demo", "password");
String filename = "ftpp.png";
String filePath = context.getFilesDir().getPath() + filename;
fos = new FileOutputStream(filePath);
client.retrieveFile("/pub/example/" + "KeyGenerator.png", fos);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fos != null) {
fos.close();
}
client.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}
解决方案
用于FTPClient.setRestartOffset
告诉服务器从之前传输中断的地方开始下载。
推荐阅读
- javafx - JavaFX 文本选择背景
- node.js - Mongoose 使用 $text search 查询错误的集合
- python - 如何使用 BeautifulSoup 获取 Twitter 帖子(推文)的日期?
- node.js - 查看 node.js 中 res.render() 传入的参数
- python - 从 Python 中的字符串列中删除停用词
- ruby - 如何使用 Sorbet 为块编写类型签名?
- php - Laravel 从模型数据生成命名路由
- lamar - 为什么 Lamar 不会在 ASP.NET Core 中注入 IServiceScopeFactory 类型?
- php - 如何使用 PDO mysql 连接修复 PHP 中的“bind_param()”错误
- tensorflow - 将 tf.dataset 写回 TFRecord