android - 如何在服务器端和客户端从带有 block 的url下载视频

标签 android web-services download

如何在服务器端和客户端从 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/

相关文章:

Android:基于位置的应用程序

android - 带有相机 2 API 的 Android Lollipop 上的 ZSL 功能

java - 我可以在 Android 中使用重叠元素吗?

rest - 在 Clojure 中开发 RESTful Web 服务的良好起点是什么?

java - 无法使用 https 协议(protocol) (ssl) 访问网络服务

web-services - 5位PIN码比大多数密码更好吗?

Java servlet 下载文件名特殊字符

Android自定义EditText View 和NextFocusDown

php - 在有内容的页面上触发文件下载

android - 下载 .apk 在手机上变成 .txt