Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I retrieve the size of a file during the downloading from an URL (using a http connection)?

I'm working on a project which download a file by using a http connection. I display a horizontal progress bar with the progress bar status during the downloading. My function looks like this:

.......
try {           
        InputStream myInput = urlconnect.getInputStream();
        BufferedInputStream buffinput = new BufferedInputStream(myInput);

        ByteArrayBuffer baf = new ByteArrayBuffer(capacity);
        int current = 0;
        while((current = buffinput.read()) != -1) {
            baf.append((byte) current);
        }
File outputfile = new File(createRepertory(app, 0), Filename);
        FileOutputStream myOutPut = new FileOutputStream(outputfile);
        myOutPut.write(baf.toByteArray());
...
}

I know in advance the size of my file so I need to retrieve the size during the downloading (in my while block). Thus I'll be able to determinate the status of the progress bar.

progressBarStatus = ((int) downloadFileHttp(url, app) * 100)/sizefile;

long downloadFileHttp(.., ..) is the name of my function.

I already try to retrieve it by using outputfile.length but his value is "1" maybe it's the number of file that I'm trying to download.

Is there any way to figure it out?

UPDATE 1

I haven't got any thread which allow me to figure this out. Currently I have got a horizontal progress bar whch displays only 0 and 100% whitout intermediate values. I think about another approach. If I know the rate of my wifi and the size of the file I can determinate the time of downloading.

I know that I can retrieve the piece of information of my Wifi connection and the size of my file to download.

Is anybody already have worked or have thread on it?

like image 212
13KZ Avatar asked Mar 05 '13 14:03

13KZ


People also ask

How can I see the size of a file while downloading?

You can install chrono and when you click on the downloading file or downloaded file there you can see the size in bytes. Just as an aside: note that this actually starts the download.

How do I download a file from http?

Generally, downloading a file from a HTTP server endpoint via HTTP GET consists of the following steps: Construct the HTTP GET request to send to the HTTP server. Send the HTTP request and receive the HTTP Response from the HTTP server. Save the contents of the file from HTTP Response to a local file.


2 Answers

AsyncTask might be the perfect solution for you:

private class DownloadFileTask extends AsyncTask<URL, Integer, Long> {
 protected Long doInBackground(URL... urls) {
    Url url = urls[0];
    //connect to url here
    .......
    try {           
        InputStream myInput = urlconnect.getInputStream();
        BufferedInputStream buffinput = new BufferedInputStream(myInput);

        ByteArrayBuffer baf = new ByteArrayBuffer(capacity);
        int current = 0;
        while((current = buffinput.read()) != -1) {
            baf.append((byte) current);
            //here you can send data to onProgressUpdate
            publishProgress((int) (((float)baf.length()/ (float)sizefile) * 100));
        }
    File outputfile = new File(createRepertory(app, 0), Filename);
    FileOutputStream myOutPut = new FileOutputStream(outputfile);
    myOutPut.write(baf.toByteArray());
    ...
 }

 protected void onProgressUpdate(Integer... progress) {
     //here you can set progress bar in UI thread
     progressBarStatus = progress;
 }

}

to start AsyncTask call here within your method

new DownloadFileTask().execute(url);
like image 31
12 revs Avatar answered Oct 12 '22 22:10

12 revs


I'll assume that you're using HttpURLConnection. In which case you need to call the getContentLength() method on urlconnect.

However, the server is not required to send a valid content length, so you should be prepared for it to be -1.

like image 121
parsifal Avatar answered Oct 12 '22 23:10

parsifal