首页 > 解决方案 > 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();
            }

        }

    }

标签: javaandroidkotlinftp

解决方案


用于FTPClient.setRestartOffset告诉服务器从之前传输中断的地方开始下载。


推荐阅读