How to assemble the file using the range header?

2019-06-06 13:53发布

问题:

I use range hader, but not create correct file.

if I send Range bytes=0-8999 file weighs 9000 bytes and correct work. if I send Range bytes=0-8999,9000-9999 file weighs 10213 bytes and NOT correct work.

File type mp3.

What could be wrong?

HttpGet first = new HttpGet("http://cs4832.vkontakte.ru/u50184979/audio/ef64581d913c.mp3");
first.addHeader("Accept-Ranges", "bytes");
first.addHeader("Range", "bytes=0-8999,9000-9999");

//first.addHeader("Accept-Ranges", "bytes");

HttpResponse response = httpclient.execute(first, localContext);


InputStream instream = response.getEntity().getContent();
File f = new File("outFile1.mp3");

OutputStream out = new FileOutputStream(f);
byte buf[] = new byte[1024];
int len;
while ((len = instream.read(buf)) > 0) {
    out.write(buf, 0, len);
}
out.close();
instream.close();

回答1:

See RFC 2616, Section 14.16:

When an HTTP message includes the content of multiple ranges (for example, a response to a request for multiple non-overlapping ranges), these are transmitted as a multipart message. The multipart media type used for this purpose is "multipart/byteranges" as defined in Appendix 19.2. See Appendix 19.6.3 for a compatibility issue.



标签: java http range