Specify the URL, calculate the file size, and specify the url File Size

Source: Internet
Author: User

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}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.