IT技术宅

青春是一个充满活力的季节,即便是我们失去了天使的翅膀,只要我们还有一颗青春的心,那么我们的生活依然能够如阳光般灿烂!......
现在位置:首页 > 代码分享 > java 图片进行base64 编码解码

java 图片进行base64 编码解码

刘振兴     代码分享      2017年06月07日     972     暂无评论  
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import java.io.*;

/**
 * @author liuzhenxing
 * @version 1.0
 * @date 2017/6/6 14:45
 */
public class ImageUtil {
    /**
     * @param imgStr base64编码字符串
     * @param path   图片路径-具体到文件
     * @return
     * @Description: 将base64编码字符串转换为图片
     * @Author:
     * @CreateTime:
     */
    public static boolean generateImage(String imgStr, String path) {
        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;
                }
            }
            OutputStream out = new FileOutputStream(path);
            out.write(b);
            out.flush();
            out.close();
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * @Description: 根据图片地址转换为base64编码字符串
     * @Author:
     * @CreateTime:
     * @return
     */
    public static String getImageStr(String imgFile) {
        InputStream inputStream = null;
        byte[] data = null;
        try {
            inputStream = new FileInputStream(imgFile);
            data = new byte[inputStream.available()];
            inputStream.read(data);
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 加密
        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(data);
    }
    /**
     * 示例
     */
    public static void main(String[] args) {
        String strImg = getImageStr("D:/1.jpg");
        System.out.println(strImg);
        generateImage("iVBORw0KGgoAAAANSUhEUgAAAC8AAAAvCAYAAABzJ5OsAAAKqklEQVRoBc1aC1CVxxU+Fy4gIChPDeIDfCAqPkGNb4hJbRpijCY1k2qsTZw0YzO0nbRNo2NDJpnU2DzsNGPNw8bW2qaNbYxJHYOK+MIg0ZioKMbw8AGIIF7ecKHn23v3Z/e//4VLYj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6UiREhLiHCphmv9rolM0xMb+QV5MlWc/Oxgi4VoczFXKqyAd56F9wBG4YO8iziRHhcCDhDWghOElvjuFiBpIIqcY0nP/Fyb7TWTYgrx0U5yOtv5cYoTsOFEmCMW4/oIOPF/o0CDnrt1jbr+esN3UXjOlXPEfvDKELeLhw0JSiboewblKkdDMJdUnTwRrsOfJ3ZwHwvE/wCBamFhoBJqhjFGrmnAWi6M2bgBwqDLf7iDrktY/omBTSb67rddLBnxX5/Q5VbwoON6AAAAAElFTkSuQmCC", "D:/666.jpg");
    }
评论一下 分享本文 赞助本站 联系站长
IT技术宅
 挤眼 亲亲 咆哮 开心 想想 可怜 糗大了 委屈 哈哈 小声点 右哼哼 左哼哼 疑问 坏笑 赚钱啦 悲伤 耍酷 勾引 厉害 握手 耶 嘻嘻 害羞 鼓掌 馋嘴 抓狂 抱抱 围观 威武 给力
提交评论

清空信息
关闭评论