英文:
Android How to retry downloading automatically from where it left if internet connection goes away using FTP protocol
问题
以下是用于在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();
}
}
}
请注意,此代码示例没有包含自动重试功能。要实现自动重试,您需要在捕获到连接错误时添加重试逻辑,并在互联网重新连接后继续下载。这可能需要使用后台服务或异步任务来实现。
英文:
Below Is the code for downloading file using FTP in android. It is working fine but in case internet goes off during download, it doesn't retry.
I want it should automatically retry whenever internet comes back and should start downloading from there, where it left. Please help me.
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();
}
}
}
答案1
得分: 1
使用FTPClient.setRestartOffset
来告诉服务器从传输中断的地方开始下载。
英文:
Use FTPClient.setRestartOffset
to tell the server to start the download from place, where the transfer was interrupted before.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论