Java URL Read getFilesizeFromUrl(String urlString)

Here you can find the source of getFilesizeFromUrl(String urlString)

Description

get Filesize From Url

License

Open Source License

Declaration

public static int getFilesizeFromUrl(String urlString)
            throws IllegalStateException, IOException 

Method Source Code

//package com.java2s;
/*****************************************************************************
 * This file is part of the Prolog Development Tool (PDT)
 * //from   w  ww. j  a v  a  2  s  . c o m
 * Author: Andreas Becker
 * WWW: http://sewiki.iai.uni-bonn.de/research/pdt/start
 * Mail: pdt@lists.iai.uni-bonn.de
 * Copyright (C): 2012, CS Dept. III, University of Bonn
 * 
 * All rights reserved. This program is  made available under the terms
 * of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 * 
 ****************************************************************************/

import java.io.IOException;

import java.net.HttpURLConnection;
import java.net.URL;

public class Main {
    public static int getFilesizeFromUrl(String urlString)
            throws IllegalStateException, IOException {

        URL url = new URL(urlString.replace(" ", "%20"));
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");
        conn.connect();

        int responseCode = conn.getResponseCode();

        if (responseCode == HttpURLConnection.HTTP_OK) {
            return conn.getContentLength();
        } else {
            throw new IllegalStateException("HTTP response: "
                    + responseCode);
        }
    }
}

Related

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