Here you can find the source of getRemoteFileSize(URL url)
public static int getRemoteFileSize(URL url) throws IOException
//package com.java2s; /******************************************************************************* * Copyright (c) 2014 Liviu Ionescu./* w w w. ja va 2s .c om*/ * All rights reserved. This program and the accompanying materials * are 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 * * Contributors: * Liviu Ionescu - initial version *******************************************************************************/ import java.io.IOException; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLConnection; public class Main { public static int getRemoteFileSize(URL url) throws IOException { URLConnection connection; while (true) { connection = url.openConnection(); if (connection instanceof HttpURLConnection) { int responseCode = ((HttpURLConnection) connection).getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { break; } else { if (responseCode == HttpURLConnection.HTTP_MOVED_TEMP || responseCode == HttpURLConnection.HTTP_MOVED_PERM || responseCode == HttpURLConnection.HTTP_SEE_OTHER) { String newUrl = connection.getHeaderField("Location"); url = new URL(newUrl); // System.out.println("Redirect to URL : " + newUrl); } else { throw new IOException("Failed to open connection, response code " + responseCode); } } } } int length = connection.getContentLength(); if (connection instanceof HttpURLConnection) { ((HttpURLConnection) connection).disconnect(); } return length; } }