Base64字节码与图片的转换㊙️

    技术2025-02-18  19

    前言

    Base64是网络上最常见的用于传输8Bit字节码的编码方式之一

    一、将图片转换成base64

    java代码: public class Base64Util { /** * 图片转为Base64字节码 * @param path 图片路径 * @return 返回base64字节码 */ public static String imageToBase64(String path) { byte[] data = null; try { InputStream in = new FileInputStream(path); data = new byte[in.available()]; in.read(data); in.close(); } catch (IOException e) { e.printStackTrace(); } Base64 base64 = new Base64(); return base64.encodeToString(data); } /** * Base64字节码转图片 * @param base64Str 字节码存储路径 * @param path 文件存储路径 * @return 返回true或者false */ public static boolean base64ToImage(String base64Str, String path) { if (base64Str == null){ return false; } Base64 base64 = new Base64(); try { byte[] bytes = base64.decodeBase64(base64Str); for (int i = 0; i < bytes.length; ++i) { if (bytes[i] < 0) { bytes[i] += 256; } } File img = new File(path); if (!img.getParentFile().exists()) { img.getParentFile().mkdirs(); } OutputStream out = new FileOutputStream(path); out.write(bytes); out.flush(); out.close(); return true; } catch (Exception e) { return false; } } }

    二、将base转给图片标签使用

    这个后期补充,抱歉了,目前还没写前端东西

    感谢大家支持。这里都是一些能看得懂,用的上手的文章,没有说都是一些底层,所谓的干活文章。

    😁 作者:Teddy (公众号:鸡仓故事汇) ok!到这里就大功告成,小编(Teddy)在这里先感谢大家的到来。 虽然不是太详细,小编已经很努力,给小编来个一键三连(点赞,关注,收藏),小编会越来越努力。。。

    Processed: 0.010, SQL: 9