zoukankan      html  css  js  c++  java
  • 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); 

    }

  • 相关阅读:
    Linux系统常见的压缩与打包
    java 语言规范 java language specifications
    java 枚举
    github邮箱验证技巧
    关于 python
    博客园 编程基础 精华
    fiddler
    一个牛人写的博客
    使用xmarks同步 chrome ie firefox safari书签
    linux 中的 tar 解压
  • 原文地址:https://www.cnblogs.com/bwl914/p/12486159.html
Copyright © 2011-2022 走看看