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

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...