简体   繁体   中英

Find out the size of html page

I am using the following code to open an input stream from a website url. I wanted to know if there is any way to find out the size of html source before I open the stream.

try
{
    URL u = new URL(url);
    InputStreamReader isr = new InputStreamReader(u.openStream());
    BufferedReader reader = new BufferedReader(isr);
    String str;
    while ((str = reader.readLine()) != null) {
        webContent += str;
    }
    reader.close();
}
catch (IOException e)
{
    Console.print(e.getMessage());
}

Try this:

URL u = new URL(url);
URLConnection conn = u.openConnection();
int length = conn.getContentLength();
InputStreamReader isr = new InputStreamReader(conn.getInputStream());
// ...

Now length will contain the Content-Length of the requested resource in bytes. Note that this will not work for chunked transfers, in which case you will need to keep reading until you reach the end of the stream to determine its length.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM