Specify the URL, calculate the file size, and specify the url File Size
- Replace the http://www.baidu.com with the specified URL to get the file size.
- The file size has been converted to KB.
- The url object uses openconnection () to open the connection. Obtain the URLConnection Class Object and connect it with the connect () method of the URLConnection class object.
1 import java.net. URL; 2 import java.net. URLConnection; 3 import java. text. decimalFormat; 4 5/* 6 calculate the file size of the specified URL 7 */8 public class FlowTool {9 public static void main (String [] args) throws Exception {10 URL url = new URL ("http://www.baidu.com"); 11 URLConnection uc = url. openConnection (); 12 String fileSize = new DecimalFormat ("0.00 "). format (float) uc. getContentLength ()/1024); 13 System. out. println ("file size:" + fileSize + "KB"); 14} 15}