有什么方法可以从 URL 字节流的特定位置开始读取吗?

Is there any way to start reading from specific position of an URL byte stream?

我的想法是把一个大的响应文本分成小的部分来同时加载它们。

以下代码帮助我从 URL 打开流,但我想从多线程加载其全部内容以优化性能,然后我将它们合并为一个结果。但是方法return一个ReadableByteChannel不能指定开始位置,我必须线性传输它:

URL url = new URL("link");
InputStream fromStream = url.openStream();
ReadableByteChannel fromChannel = Channels.newChannel(fromStream);

有没有办法像SeekableByteChannel那样指定位置(好像这个界面只对文件有效)?谢谢你:D

如果您可以在请求成为流之前对其进行操作,那么是的,您可以使用 Range http header 来指定您想要的数据块...

Downloading a portion of a File using HTTP Requests

否则,您将不得不手动读取不需要的数据。

Given a Java InputStream, how can I determine the current offset in the stream?