zoukankan      html  css  js  c++  java
  • 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();
            }
        }
    
  • 相关阅读:
    HDU4529 郑厂长系列故事——N骑士问题 —— 状压DP
    POJ1185 炮兵阵地 —— 状压DP
    BZOJ1415 聪聪和可可 —— 期望 记忆化搜索
    TopCoder SRM420 Div1 RedIsGood —— 期望
    LightOJ
    LightOJ
    后缀数组小结
    URAL
    POJ3581 Sequence —— 后缀数组
    hdu 5269 ZYB loves Xor I
  • 原文地址:https://www.cnblogs.com/black-spike/p/11857870.html
Copyright © 2011-2022 走看看