文件流---获取图片的宽、高、大小

来源:互联网 发布:php 替换字符串 某一段 编辑:程序博客网 时间:2024/06/15 13:39
public static Map<String,String> getImageData(String path) {
        Map<String, String> imageMap = new HashMap<String, String>();
        File picture = new File(path);
        BufferedImage sourceImg = null;
        try {
            sourceImg = ImageIO.read(new FileInputStream(picture));
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        String size = "";
        if (picture.exists() && picture.isFile()) {
            long fileS = picture.length();
            DecimalFormat df = new DecimalFormat("#.00");
            if (fileS < 1024) {
                size = df.format((double) fileS) + "BT";
            } else if (fileS < 1048576) {
                size = df.format((double) fileS / 1024) + "KB";
            } else if (fileS < 1073741824) {
                size = df.format((double) fileS / 1048576) + "MB";
            } else {
                size = df.format((double) fileS / 1073741824) + "GB";
            }
        } else if (picture.exists() && picture.isDirectory()) {
            size = "";
        } else {
            size = "0BT";
        }

        int width = sourceImg.getWidth();
        int height = sourceImg.getHeight();
        imageMap.put("size", size);
        imageMap.put("height", String.valueOf(height));
        imageMap.put("width", String.valueOf(width));
        return imageMap;
    }
原创粉丝点击