gpt4 book ai didi

Java : URLConnection setRequestProperty range issue relying on multithreaded process?

转载 作者:太空宇宙 更新时间:2023-11-04 12:09:46 32 4
gpt4 key购买 nike

我在尝试按范围读取远程文件时遇到困难。

根据以下跟踪声音,setRequestProperty 没有执行其工作(InputStream 长度对我来说是不行的,除了一个..第一个???)?

Task Runnable ID = 0 --> Requested Range Property = {Range=[bytes=0-93969]}Task Runnable ID = 1 --> Resquested Range Property = {Range=[bytes=93970-187939]}Task Runnable ID = 2 --> Requested Range Property = {Range=[bytes=187940-281909]}Task Runnable ID = 3 --> Resquested Range Property = {Range=[bytes=281910-375883]}Task Runnable ID = 0 --> InputStream Lenght = 93970 / StartByte = 0 / CurrentByte = 0 / EndByte = 93969Task Runnable ID = 1 --> InputStream Length = 375883 / StartByte = 93970 / CurrentByte = 93970 / EndByte = 187939Task Runnable ID = 3 --> InputStream Length = 375883 / StartByte = 281910 / CurrentByte = 281910 / EndByte = 375883Task Runnable ID = 2 --> InputStream Length = 375883 / StartByte = 187940 / CurrentByte = 187940 / EndByte = 281909

我的代码是:

public class TaskRunnable implements Runnable {    private static final int    BUFFER_SIZE     = 4092;    private long                startByte;    private long                currentByte;    private long                endByte;    private Task                task;    private static int          idCounter       = 0;    private int                 id;    @SuppressWarnings("unused")    private TaskRunnable() {    }    public TaskRunnable(Task task, long startByte, long endByte) {        this.startByte = startByte;        this.endByte = endByte;        this.task = task;        this.currentByte = startByte;        this.id = idCounter++;    }    @Override    public void run() {        Thread.currentThread().setName("Download Runnable");        Authenticator authenticator = task.getManager().getAuthenticator();        if (authenticator != null) {            Authenticator.setDefault(authenticator);        }        File targetFile;        synchronized (this) {            targetFile = new File(task.getTargetFile().getAbsolutePath());        }        BufferedInputStream bufferedInputStream = null;        byte[] buf = new byte[BUFFER_SIZE];        URLConnection urlConnection = null;        try {            URL _url = new URL(task.getSourceFileUrl().toString());            Proxy proxy = task.getManager().getProxy();            if (proxy != null) {                urlConnection = _url.openConnection(proxy);            } else {                urlConnection = _url.openConnection();            }            urlConnection.setRequestProperty("Range", "bytes=" + currentByte + "-" + endByte);            System.out.println("Task Runnable ID = " + id + " --> Requested Range Property = " + urlConnection.getRequestProperties().toString());            bufferedInputStream = new BufferedInputStream(urlConnection.getInputStream());            int len = 0;            while (bufferedInputStream.read() != -1) {                len++;            }            System.out.println("Task Runnable ID = " + id + " --> InputStream Length = " + len + " / StartByte = " + startByte + " / CurrentByte = " + currentByte + " / EndByte = " + endByte);            bufferedInputStream.close();        } catch (IOException e) {            e.printStackTrace();        }    }

显然这是我的错,但无法找出问题所在。非常欢迎提供帮助。谢谢!

注意:如果使用相同的代码但基于单线程,则一切运行正常。

最佳答案

我怀疑内部 URLConnection 状态发生了并发修改。您是否尝试过调用 URLConnection.setUseCaches(false)

更新

另一个可能影响 URLConnection 对象重用的全局缓存是 ResponseCache 。您应该在初始化时通过执行禁用它

ResponseCache.setDefault(null);

关于Java : URLConnection setRequestProperty range issue relying on multithreaded process?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39957709/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com