读取网络流获取文件大小



package uxsino;


import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.text.DecimalFormat;


//http://www.runoob.com/wp-content/themes/runoob/assets/img/newlogo.png
//http://test.pi-brand.com/jeesite/userfiles/1/images/cms/product/2016/12/product_logo_0.png


public class Main {
public static int getStrByUrlConnection(String spec) {
        int result = 0;
        try {
            URL url = new URL(spec);
            URLConnection urlConnection = url.openConnection();
            urlConnection.setDoInput(true);
            InputStream inputStream = urlConnection.getInputStream();
            result = stream2Str(inputStream);
        } catch (IOException e) {
            return 0;
        }
        return result;
    }


    private static int stream2Str(InputStream inputStream) {
        byte[] buf = new byte[1024];
        int len = -1;
        int size = 0;
        try {
            while ((len = inputStream.read(buf)) != -1) {
            size += len;
            }
        } catch (IOException e) {
            return 0;
        }
        System.out.println(formetFileSize(size));
        return size;
    }
    
    public static String formetFileSize(long fileS) { 
        DecimalFormat df = new DecimalFormat("#.00"); 
        String fileSizeString = ""; 
        if (fileS < 1024) { 
            fileSizeString = df.format((double) fileS) + "B"; 
        } else if (fileS < 1048576) { 
            fileSizeString = df.format((double) fileS / 1024) + "K"; 
        } else if (fileS < 1073741824) { 
            fileSizeString = df.format((double) fileS / 1048576) + "M"; 
        } else { 
            fileSizeString = df.format((double) fileS / 1073741824) + "G"; 
        } 
        return fileSizeString; 
    } 
    
    public static void main(String[] args) {
    getStrByUrlConnection("http://down.liangchan.net/EXCEL2003.zip");
    }
}






package uxsino;


import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.text.DecimalFormat;




public class News {
    public static void downloadNet(String path) throws MalformedURLException {
        // 下载网络文件
        long bytesum = 0;
        long byteread = 0;
        
        URL url = new URL(path);
        
        try {
            URLConnection conn = url.openConnection();
            InputStream inStream = conn.getInputStream();


            byte[] buffer = new byte[1024];
            while ((byteread = inStream.read(buffer)) != -1) {
                bytesum += byteread;
            }
            System.out.println(formetFileSize(bytesum));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) throws MalformedURLException {
        downloadNet("http://down.liangchan.net/EXCEL2003.zip");
    }
    
    public static String formetFileSize(long fileS) { 
        DecimalFormat df = new DecimalFormat("#.00"); 
        String fileSizeString = ""; 
        if (fileS < 1024) { 
            fileSizeString = df.format((double) fileS) + "B"; 
        } else if (fileS < 1048576) { 
            fileSizeString = df.format((double) fileS / 1024) + "K"; 
        } else if (fileS < 1073741824) { 
            fileSizeString = df.format((double) fileS / 1048576) + "M"; 
        } else { 
            fileSizeString = df.format((double) fileS / 1073741824) + "G"; 
        } 
        return fileSizeString; 
    } 
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值