Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to check progress of URLconnection.getInputStream()?

I want to check progress of downloading file by URLconnection. Is it possible or should I use another library? This is my urlconnection function:

public static String sendPostRequest(String httpURL, String data) throws UnsupportedEncodingException, MalformedURLException, IOException {
    URL url = new URL(httpURL);

    URLConnection conn = url.openConnection();
    //conn.addRequestProperty("Content-Type", "text/html; charset=iso-8859-2");
    conn.setDoOutput(true);
    OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
    wr.write(data);
    wr.flush();

    BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream(), "ISO-8859-2"));
    String line, all = "";
    while ((line = rd.readLine()) != null) {
        all = all + line;
    }
    wr.close();
    rd.close();
    return all;
}

I understand that whole file is downloaded in this line (or worng)?:

BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream(), "ISO-8859-2"));

So is it possible to do this in this code?

like image 233
latata Avatar asked Mar 01 '11 23:03

latata


People also ask

What does connection getInputStream do?

Calling getInputStream() signals that the client is finished sending it's request, and is ready to receive the response (per HTTP spec). It seems that the URLConnection class has this notion built into it, and must be flush()ing the output stream when the input stream is asked for.

What does getContentType return?

getContentType. Returns the value of the content-type header field. Returns: the content type of the resource that the URL references, or null if not known.

How does Httpsurlconnection work?

It overrides the getHeaderField method of URLConnection class. Returns true or false depending on whether automatic instance redirection is set or not. Retrieves the permission required to connect to a destination host and port. Used to retrieve the response status from server.


1 Answers

Just check if the HTTP Content-Length header is present in the response.

int contentLength = connection.getContentLength();

if (contentLength != -1) {
    // Just do (readBytes / contentLength) * 100 to calculate the percentage.
} else {
    // You're lost. Show "Progress: unknown"
}

Update as per your update, you're wrapping the InputStream inside a BufferedReader and reading inside a while loop. You can count the bytes as follows:

int readBytes = 0;

while ((line = rd.readLine()) != null) {
    readBytes += line.getBytes("ISO-8859-2").length + 2; // CRLF bytes!!
    // Do something with line.
}

The + 2 is to cover the CRLF (carriage return and linefeed) bytes which are eaten by BufferedReader#readLine(). More clean approach would be to just read it by InputStream#read(buffer) so that you don't need to massage the bytes forth and back from characters to calculate the read bytes.

See also:

  • How to use java.net.URLConnection to fire and handle HTTP requests?
like image 97
BalusC Avatar answered Sep 20 '22 23:09

BalusC