java 根據圖片地址獲取到圖片的大小,單位kb或者Mb


/**
     * byte(字節)根據長度轉成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"); 
    }

/** 

* @Title: pathSize  

 

*@param imgPath 

 

*@return  根據圖片地址返回圖片大小kb或者 Mb    

* @return String   

 * @throws  

* @add (default no) 

*/ 

public 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); 

}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM