如何在服务器端和客户端从 url 中下载带有 block 的视频。
HttpURLConnection connection =
(HttpURLConnection) url.openConnection();
// Specify what portion of file to download.
connection.setRequestProperty("Range",
"bytes=" + downloaded + "-");
// Connect to server.
connection.connect();
// Make sure response code is in the 200 range.
if (connection.getResponseCode() / 100 != 2) {
error();
}
// Check for valid content length.
int contentLength = connection.getContentLength();
if (contentLength < 1) {
error();
}
/* Set the size for this download if it
hasn't been already set. */
if (size == -1) {
size = contentLength;
stateChanged();
}
// Open file and seek to the end of it.
File f = new File("/sdcard/Sample");
if(f.exists())
{
}else
{
f.mkdir();
}
file = new RandomAccessFile(f.getAbsolutePath()+"/"+getFileName(url), "rw");
file.seek(downloaded);
stream = connection.getInputStream();
在这里我想下载一个像 block 一样的文件......如果 block 来自服务器和我的客户端我需要下载并合并那个完整的视频文件
最佳答案
假设您使用 HTTP 下载,您需要使用 HEAD http 动词和 RANGE http header 。
HEAD 将为您提供文件大小(如果可用),然后 RANGE 可让您下载一个字节范围。
获得文件大小后,将其分成大小大致相等的 block ,并为每个 block 生成下载线程。完成所有操作后,以正确的顺序写入文件 block 。
有关示例的详细信息,请阅读此 SO 答案 https://stackoverflow.com/a/16788587/5231773
关于android - 如何在服务器端和客户端从带有 block 的url下载视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21930136/