【问题标题】:JAVA - Resume/Pause https connection download [closed]JAVA - 恢复/暂停 https 连接下载 [关闭]
【发布时间】:2020-08-15 10:04:02
【问题描述】:

我有这样的代码

    public void downloadZip(String URL, String fileName) {

    try {
        java.net.URL url = new URL(URL);
        URLConnection conn = url.openConnection();
        InputStream in = conn.getInputStream();
        FileOutputStream out = new FileOutputStream(save + "/" + fileName);
        byte[] b = new byte[4096];
        int count;
        while ((count = in.read(b)) >= 0) {
            out.write(b, 0, count);
        }
        out.flush(); out.close(); in.close();

    } catch (IOException e) {
        e.printStackTrace();
    }
}

如何重写它才能继续/停止加载?

【问题讨论】:

  • 您会发现,当您尽可能具体地解释您的问题时,StackOverflow 对您的帮助最大。这个问题看起来像你要求我们为你做你的工作。请自己尝试一些解决方案,并告诉我们您已经尝试过什么,为什么它不起作用等。祝您好运。

标签: java https download zip desktop


【解决方案1】:

您需要使用 SwingUtilities 使用单独的线程。如果要停止,请在 GUI 线程中设置一个标志。如果要恢复,可以重新打开 URL,使用 InputStream 的 skip 方法从离开的地方开始。要暂停,恢复,您可以使用信号。见http://tutorials.jenkov.com/java-concurrency/thread-signaling.html

您需要自学多线程。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-03
  • 2012-11-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多