问题描述
以下是在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
告诉服务器从以前传输中断的地方开始下载。