Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Possible to determine size of file to be downloaded?

I currently have the following code which reads a file located on the web and writes it to a file on the phone:

InputStream inputStream = new URL(sourceFileWebAddress).openStream();
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
FileOutputStream fileOutputStream = new FileOutputStream(targetFile);

int count;
byte buffer[] = new byte[1024];

while ((count = bufferedInputStream.read(buffer, 0, buffer.length)) != -1)
  fileOutputStream.write(buffer, 0, count);

Does anyone know whether it is possible (using the setup above or otherwise) to determine the total number of bytes which are to be read before commencing with the download (in order to publish percentage progress to the user as the download proceeds)?

like image 861
Adil Hussain Avatar asked Jun 06 '12 12:06

Adil Hussain


People also ask

How do I know the size of a file before downloading?

you can get a header called Content-Length form the HTTP Response object that you get, this will give you the length of the file. you should note though, that some servers don't return that information, and the only way to know the actual size is to read everything from the response.

How do you determine a file size?

Locate the file or folder whose size you would like to view. Click the file or folder. Press Command + I on your keyboard. A window opens and shows the size of the file or folder.

How do I check the size of a file in MB?

You can retrieve the length of the file with File#length(), which will return a value in bytes, so you need to divide this by 1024*1024 to get its value in mb.


3 Answers

use the getContentLength method of URLConnection

URL url = new URL(sourceFileWebAddress);
URLConnection connection = url.openConnection();
connection.connect();

int fileLenth = connection.getContentLength();

InputStream inputStream = url.openStream();
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
like image 101
Ed. Avatar answered Oct 17 '22 15:10

Ed.


TO determine the total number of bytes which are to be read before commencing with the download, one way is to only get the response headers by sending a HTTP HEAD request as follows:

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;

public class SimpleHTTPRequest {

  public static void main(String[] args) {
      HttpURLConnection connection = null;
      URL serverAddress = null;

      try {
          String sourceFileWebAddress = "http://localhost:8080/mycontents";
          serverAddress = new URL(sourceFileWebAddress);
          //set up out communications stuff
          connection = null;

          //Set up the initial connection
          connection = (HttpURLConnection)serverAddress.openConnection();

          //HEAD request will make sure that the contents are not downloaded.
          connection.setRequestMethod("HEAD");  

          connection.connect();
          System.out.println("========"+connection.getContentLength());

      } catch (MalformedURLException e) {
          e.printStackTrace();
      } catch (ProtocolException e) {
          e.printStackTrace();
      } catch (IOException e) {
          e.printStackTrace();
      }
      finally
      {
          //close the connection, set all objects to null
          connection.disconnect();

          connection = null;
      }
  }
}

This will print the size of the content to be downloaded without actually downloading the contents.

like image 3
Santosh Avatar answered Oct 17 '22 16:10

Santosh


try this out using URLConnnection

URLConnection connection = new URL(sourceFileWebAddress).openConnection();
InputStream stream = connection.getInputStream();

System.out.println("total size: "+connection.getContentLength();//size

BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
FileOutputStream fileOutputStream = new FileOutputStream(targetFile);

int count;
byte buffer[] = new byte[1024];

while ((count = bufferedInputStream.read(buffer, 0, buffer.length)) != -1)
  fileOutputStream.write(buffer, 0, count);
like image 3
Mohammed Azharuddin Shaikh Avatar answered Oct 17 '22 15:10

Mohammed Azharuddin Shaikh