base64字符串转化成图片

/**
 * base64字符串转化成图片
 *
 * @param base64
 * @return
 */
public static File base64ToFile(String base64) throws Exception {
    if (base64.contains("data:image")) {
        base64 = base64.substring(base64.indexOf(",") + 1);
    }
    base64 = base64.toString().replace("\r\n", "");
    //创建文件目录
    String prefix = ".jpeg";
    File file = File.createTempFile(UUID.randomUUID().toString(), prefix);
    BufferedOutputStream bos = null;
    FileOutputStream fos = null;
    try {
        BASE64Decoder decoder = new BASE64Decoder();
        byte[] bytes = decoder.decodeBuffer(base64);
        fos = new FileOutputStream(file);
        bos = new BufferedOutputStream(fos);
        bos.write(bytes);
    } finally {
        if (bos != null) {
            try {
                bos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (fos != null) {
            try {
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return file;
}

通过读取base64格式图片 并获取其width及height的方式,来判断判断当前文件是否图片
public static boolean determineIfThePictureIsValidLt(String Base64Photo) throws IOException {
    if (StringUtils.isNotEmpty(Base64Photo)) {
        String data = Base64Photo.split("base64,")[0];
        String base64Image = Base64Photo.replaceFirst(data, "");
        if (StringUtils.isNotEmpty(base64Image)) {
            byte[] decoder = new BASE64Decoder().decodeBuffer(base64Image);
            InputStream is = new ByteArrayInputStream(decoder);
            BufferedImage read = ImageIO.read(is);
            if (read == null || read.getWidth(null) <= 0 || read.getHeight(null) <= 0) {
                return false;
            }
            return true;
        }
    }
    return false;
}

/**
 * 通过读取文件并获取其width及height的方式,来判断判断当前文件是否图片,这是一种非常简单的方式。
 *
 * @param imageFile
 * @return
 */
public static boolean isImage(File imageFile) {
    if (!imageFile.exists()) {
        return false;
    }
    Image img = null;
    try {
        img = ImageIO.read(imageFile);
        if (img == null || img.getWidth(null) <= 0 || img.getHeight(null) <= 0) {
            return false;
        }
        return true;
    } catch (Exception e) {
        return false;
    } finally {
        img = null;
    }
}

/**
 * 通过读取文件并获取其width及height的方式,来判断判断当前文件是否图片,这是一种非常简单的方式。
 *
 * @param imageFile
 * @return
 */
public static boolean isImage(File imageFile) {
    if (!imageFile.exists()) {
        return false;
    }
    Image img = null;
    try {
        img = ImageIO.read(imageFile);
        if (img == null || img.getWidth(null) <= 0 || img.getHeight(null) <= 0) {
            return false;
        }
        return true;
    } catch (Exception e) {
        return false;
    } finally {
        img = null;
    }
}

// 1.需要计算文件流大小,首先把头部的data:image/png;base64,(注意有逗号)去掉。
String str = Base64Photo.substring(22);
//2.找到等号,把等号也去掉
Integer equalIndex = str.indexOf("=");
if (str.indexOf("=") > 0) {
    str = str.substring(0, equalIndex);
}
//3.原来的字符流大小,单位为字节
Integer strLength = str.length();
//4.计算后得到的文件流大小,单位为字节
Integer size = strLength - (strLength / 8) * 2;

Path write = Files.write(Paths.get(Base64Photo), Base64.getDecoder().decode(Base64Photo), StandardOpenOption.CREATE);
Image img = null;
try {
    img = ImageIO.read((ImageInputStream) write);
    if (img == null || img.getWidth(null) <= 0 || img.getHeight(null) <= 0) {
        return false;
    }
    return true;
} catch (Exception e) {
    return false;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不敲代 码的程序员

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值