美文网首页程序员
java根据图片地址获取图片的字节数

java根据图片地址获取图片的字节数

作者: 梦将空 | 来源:发表于2018-10-11 10:38 被阅读0次

1、获取本地图片的的字节数

/**
* 获取本地图片的字节数
* @param imgPath
* @return
*/
public static String pathSize(String imgPath) {     
    File file = new File(imgPath);      
    FileInputStream fis;        
    int fileLen = 0;        
    try {           
        fis = new FileInputStream(file);            
        fileLen = fis.available();      
    } 
    catch (FileNotFoundException e) 
    {           
        e.printStackTrace();                
    } catch (IOException e) 
    {           
            e.printStackTrace();        
    }       
    return bytes2kb(fileLen);
}

/**
 * 将获取到的字节数转换为KB,MB模式
 * @param bytes
 * @return
 */
public static String bytes2kb(long bytes){
    BigDecimal filesize = new BigDecimal(bytes);
    BigDecimal megabyte = new BigDecimal(1024 * 1024);
    float returnValue = filesize.divide(megabyte, 2, BigDecimal.ROUND_UP).floatValue();
    if(returnValue > 1)
        return (returnValue + "MB");
    BigDecimal kilobyte = new BigDecimal(1024);
    returnValue = filesize.divide(kilobyte, 2, BigDecimal.ROUND_UP).floatValue();
    return (returnValue + "KB");
}

public static void main(String[] args) {
    String imgUrl="E:\\vacations.ctrip.com_852377.4201748744.jpg";
    String pathSize = pathSize(imgUrl);
    System.out.println("获取到图片的大小: " + pathSize);
}

测试结果


测试.png

原始图片的大小


原始图片.png

2、获取网络图片地址的字节数

/**
 * 根据图片地址获取图片信息
 * @param urlPath   网络图片地址
 * @return
 */
public static byte[] getImageFromURL(String urlPath) { 
    byte[] data = null; 
    InputStream is = null; 
    HttpURLConnection conn = null; 
    try { 
        URL url = new URL(urlPath); 
        conn = (HttpURLConnection) url.openConnection(); 
        conn.setDoInput(true); 
        conn.setRequestMethod("GET"); 
        conn.setConnectTimeout(6000); 
        is = conn.getInputStream(); 
        if (conn.getResponseCode() == 200) { 
            data = readInputStream(is); 
        } else{ 
            data=null; 
        } 
    } catch (MalformedURLException e) { 
        e.printStackTrace(); 
    } catch (IOException e) { 
        e.printStackTrace(); 
    } finally { 
        try { 
            if(is != null){ 
                is.close(); 
            }                
        } catch (IOException e) { 
            e.printStackTrace(); 
        } 
        conn.disconnect();           
    } 
    return data; 
} 

/**
 * 将流转换为字节
 * @param is
 * @return
 */
public static byte[] readInputStream(InputStream is) { 
    ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
    byte[] buffer = new byte[1024]; 
    int length = -1; 
    try { 
        while ((length = is.read(buffer)) != -1) { 
            baos.write(buffer, 0, length); 
        } 
        baos.flush(); 
    } catch (IOException e) { 
        e.printStackTrace(); 
    } 
    byte[] data = baos.toByteArray(); 
    try { 
        is.close(); 
        baos.close(); 
    } catch (IOException e) { 
        e.printStackTrace(); 
    } 
    return data; 
} 


测试结果


测试.png

查看源码
链接: https://pan.baidu.com/s/1GhrX2WIlwPqPQ9DhlC786w 提取码: v854

相关文章

网友评论

    本文标题:java根据图片地址获取图片的字节数

    本文链接:https://www.haomeiwen.com/subject/lqgvaftx.html