我thinkt你問的是HTTP報頭
Accept-Ranges: bytes
Range: bytes=0-8999
這將指示服務器只發送了一些文件。然後您將順序讀取流。
又見How to assemble the file using the range header?
編輯:例如
這似乎是工作
public static void main(String[] args) throws MalformedURLException, IOException {
URLConnection conn = new URL("http://ftp.debian.org/debian/dists/stable/Contents-i386.gz")
.openConnection();
conn.addRequestProperty("Accept-Ranges","bytes");
conn.addRequestProperty("Range", "bytes=8000000-16000000");
InputStream input = conn.getInputStream();
List<String> serverranges = conn.getHeaderFields().get("Accept-Ranges");
boolean ispartial = serverranges != null && serverranges.get(0).equals("bytes");
byte[] b = new byte[1024];
int l ;
System.out.println(ispartial);
while((l=input.read(b, 0, b.length))>0){
// if isPartial=true, we have server support. We received partial file.
//do stuff with b,l
}
}
要注意,並非所有的服務器都支持這一點很重要,所以檢查isPartial變量。如果它是假的。服務器不支持部分範圍,並會給你文件的開始。
簡答:不,因爲流不是隨機訪問。 –
如果你正在尋找自己的下載管理器,你應該閱讀HTTP範圍請求。流不按照你想要的方式工作。 –
然後是否可以指定請求上的字節位置? 所以我將能夠使用2個或更多線程一次讀取同一個流的不同位置的數據。它會帶來更多的性能增益嗎? @PhilippReichart –