![](/img/trans.png)
[英]java.lang.IllegalArgumentException: while adding '\n\ to URLConnection property setRequestProperty
[英]Java : URLConnection setRequestProperty range issue relying on multithreaded process?
我試圖通過遠程文件讀取范圍。
根據以下跟蹤聲音setRequestProperty沒有執行它的工作(InputStream Length對我來說除了一個......第一個???)?
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 = 93969 Task Runnable ID = 1 --> InputStream Length = 375883 / StartByte = 93970 / CurrentByte = 93970 / EndByte = 187939 Task Runnable ID = 3 --> InputStream Length = 375883 / StartByte = 281910 / CurrentByte = 281910 / EndByte = 375883 Task 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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.