java根據圖片地址獲取圖片的字節(jié)數

1、獲取本地圖片的的字節(jié)數

/**
* 獲取本地圖片的字節(jié)數
* @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);
}

/**
 * 將獲取到的字節(jié)數轉換為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、獲取網絡圖片地址的字節(jié)數

/**
 * 根據圖片地址獲取圖片信息
 * @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; 
} 

/**
 * 將流轉換為字節(jié)
 * @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

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容