zoukankan      html  css  js  c++  java
  • base642photo

    /**
         * pic to base64Str
         * @param path 读取路径
         * @return
         */
        public static String GetImageStr(String path) {//转化成base64字符串
            String imgFile = path;
            InputStream in = null;
            byte[] data = null;
            try {
                in = new FileInputStream(imgFile);
                data = new byte[in.available()];
                in.read(data);
                in.close();
                BASE64Encoder encoder = new BASE64Encoder();
                return encoder.encode(data);// 返回Base64编码过的字节数组字符串
            } catch (IOException e) {
                logger.error("图片转换base64字符串出错"+e.getMessage());
            }
            return "";
        }
        /**
         *  base64Str to pic
         * @param imgStr base64 字符串
         * @param path 存储路径
         * @return
         */
        public static boolean GenerateImage(String imgStr,String path) { // 对字节数组字符串进行Base64解码并生成
            if (imgStr == null) {
                return false;
            }
            BASE64Decoder decoder = new BASE64Decoder();
            try {
                byte[] b = decoder.decodeBuffer(imgStr);
                for (int i = 0; i < b.length; ++i) {
                    if (b[i] < 0) {// 调整异常数据
                        b[i] += 256;
                    }
                }
                File file = new File(path);
                if (!file.getParentFile().exists()){
                    file.getParentFile().mkdirs();
                }

                OutputStream out = new FileOutputStream(new File(path));
                out.write(b);
                out.flush();
                out.close();
                return true;
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }

  • 相关阅读:
    以链表为载体学习C++(1)
    以链表为载体学习C++(2)
    20191304商苏赫Python程序设计实验一
    Python实验二 20191304商苏赫
    七大基本排序算法之插入排序
    七大基本排序算法之归并排序
    《富爸爸,穷爸爸》
    七大基本排序算法之冒泡排序
    七大基本排序算法之选择排序
    七大基本排序算法之希尔排序
  • 原文地址:https://www.cnblogs.com/zsqfightyourway/p/9449303.html
Copyright © 2011-2022 走看看