RandomAccessFile over HTTP

2019-07-08 09:35发布

问题:

It's easy to open a connection to a remote asset using URL.openStream() which returns an InputStream. What about random access?

回答1:

HTTP 1.1 allows retrieval of parts of a resource through use of Byte Ranges, but...

  • servers are not required to support it
  • you will incur extra overhead due extra request/response headers, (possibly) establishing extra network connections, etc.


回答2:

True random access is not possible via http. You can simulate it though by passing a starting point as a paramater and return a stream that represents data from that position onwards.

For example this is how progressive HTTP download works (e.g. YouTube video seek).



标签: java http