3

How to download a video from url with chunks in both server and client end.

  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();

here i want to download a file like a chunks ... if the chunks coming from server and my client end i need to download and merge that complete video file

Venus
  • 219
  • 1
  • 3
  • 13

1 Answers1

0

Assuming you're using HTTP for the download, you'll want to use the HEAD http verb and RANGE http header.

HEAD will give you the filesize (if available), and then RANGE lets you download a byte range.

Once you have the filesize, divide it into roughly equal sized chunks and spawn download thread for each chunk. Once all are done, write the file chunks in the correct order.

for detailed info with example read this SO answer https://stackoverflow.com/a/16788587/5231773

Community
  • 1
  • 1
Learn Pain Less
  • 2,274
  • 1
  • 17
  • 24