Home > Java > javaTutorial > Java Example - Get remote file size

Java Example - Get remote file size

黄舟
Release: 2017-01-20 11:47:15
Original
1625 people have browsed it

The following example demonstrates how to get the size of a remote file:

/*
 author by runoob.com
 Main.java
 */import java.net.URL;import java.net.URLConnection;public class Main {
   public static void main(String[] argv) 
   throws Exception {
      int size;
      URL url = new URL("http://www.w3cschool.cc/wp-content/themes/w3cschool.cc/assets/img/newlogo.png");
      URLConnection conn = url.openConnection();
      size = conn.getContentLength();
      if (size < 0)
      System.out.println("无法获取文件大小。");
      else
      System.out.println("文件大小为:" +
      + size + " bytes");
      conn.getInputStream().close();
   }}
Copy after login

The output result of running the above code is:

文件大小为:4261 bytes
Copy after login

The above is the Java example - Get the size of the remote file For more related content, please pay attention to the PHP Chinese website (m.sbmmt.com)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template