如何在互联网连接中断时使用FTP协议自动从上次下载位置重试下载Android。

huangapple go评论81阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2020年8月11日 15:50:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/63353792.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定