读取网络流获取文件大小

来源:互联网 发布:2016中国人工智能大会 编辑:程序博客网 时间:2024/04/30 21:24



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; 
    } 
}


0 0