• java将base64解析图片保存到本地。


    将base64解析图片保存到本地的两个方法

        /**
         * base64转图片
         * @param base64str base64码
         * @param savePath 图片路径
         * @return
         */
        public static boolean GenerateImage(String base64str, String savePath) {
            //对字节数组字符串进行Base64解码并生成图片
            if (base64str == null) {
                return false;
            }
            BASE64Decoder decoder = new BASE64Decoder();
            try {
                //Base64解码
                byte[] b = decoder.decodeBuffer(base64str);
                for (int i = 0; i < b.length; ++i) {
                    //调整异常数据
                    if (b[i] < 0) {
                        b[i] += 256;
                    }
                }
                //生成jpeg图片
                OutputStream out = new FileOutputStream(savePath);
                out.write(b);
                out.flush();
                out.close();
                return true;
            } catch (Exception e) {
                return false;
            }
        }
    
        /**
         * base64转图片
         * @param base64Code base64码
         */
        public static void convertBase64ToImage(String base64Code){
            BufferedImage image = null;
            byte[] imageByte = null;
            try {
                imageByte = DatatypeConverter.parseBase64Binary(base64Code);
                ByteArrayInputStream bis = new ByteArrayInputStream(imageByte);
                image = ImageIO.read(new ByteArrayInputStream(imageByte));
                bis.close();
                File outputfile = new File("d:\sealImg.jpg");
                ImageIO.write(image, "jpg", outputfile);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
  • 相关阅读:
    JDK中的主要包
    package

    参数传值机制
    静态初始化块
    static 关键字
    this关键字
    开发中容易造成内存泄露的操作
    通用的分代垃圾回收机制
    JVM调优和Full GC
  • 原文地址:https://www.cnblogs.com/black-spike/p/11857870.html
Copyright © 2020-2023  润新知