zoukankan      html  css  js  c++  java
  • Java如何从服务器获取文件大小?

    在Java编程中,如何从服务器获取文件大小?

    以下示例演示如何从服务器获取文件大小。

    package com.yiibai;
    
    import java.net.URL;
    import java.net.URLConnection;
    
    public class GettingFileSize {
        public static void main(String[] argv) throws Exception {
            int size;
            URL url = new URL("http://www.yiibai.com/downloads/host.txt");
            URLConnection conn = url.openConnection();
            size = conn.getContentLength();
    
            if (size < 0)
                System.out.println("Could not determine file size.");
            else
                System.out.println("The size of file is = " + size + " bytes");
            conn.getInputStream().close();
        }
    }
    
    Java

    上述代码示例将产生以下结果 -

    The size of file is = 180822 bytes
    
    Shell

    示例-2

    以下是如何从服务器获取文件大小的另一个示例。

    package com.yiibai;
    
    import java.net.URL;
    import java.net.URLConnection;
    
    public class GettingFileSize2 {
        public static void main(String[] argv) throws Exception {
            int size;
            URL url = new URL("http://www.yiibai.com/downloads/yiibaidb.zip");
            URLConnection conn = url.openConnection();
            size = conn.getContentLength();
            if (size < 0)
                System.out.println("file size is empty.");
            else
                System.out.println("File size is = " + size + " bytes");
            conn.getInputStream().close();
        }
    }
    
    Java

    上述代码示例将产生以下结果 -

    File size is = 60098 bytes
  • 相关阅读:
    【转载】深入浅出VA函数
    oracle数据库怎么创建数据库实例
    2.4 OpenEuler中C语言中的函数调用测试(选做)
    OpenEuler 中C与汇编的混合编程(选做)
    程序运行
    改进ls的实现(课下作业)
    学习笔记12
    实验四 Web服务器1socket编程
    实验四 Web服务器2
    学习6
  • 原文地址:https://www.cnblogs.com/borter/p/9617167.html
Copyright © 2011-2022 走看看