Java URL Read getFileSize(URL url)

Here you can find the source of getFileSize(URL url)

Description

Returns the size of the file located at the given URL.

License

Apache License

Parameter

Parameter Description
url the url of the file

Return

the size of the file

Declaration

public static int getFileSize(URL url) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.io.*;

import java.net.HttpURLConnection;

import java.net.URL;

public class Main {
    /**/*from  www.j a v a 2 s  .  co m*/
     * Returns the size of the file located at the given URL.
     *
     * @param url the url of the file
     *
     * @return the size of the file
     */
    public static int getFileSize(URL url) {
        HttpURLConnection conn = null;
        try {
            conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("HEAD");
            conn.getInputStream();
            return conn.getContentLength();
        } catch (IOException e) {
            return -1;
        } finally {
            conn.disconnect();
        }
    }
}

Related

  1. getBodyResponse(HttpURLConnection con)
  2. getBytes(URL url)
  3. getBytes(URL url)
  4. getFileContentFromWeb(final URL srcUrl, final FileOutputStream destination)
  5. getFileSize(String sURL)
  6. getFilesizeFromUrl(String urlString)
  7. getFromURL(String URL)
  8. getHTML(String pageURL, String encoding)
  9. getHtml(String url)