博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
图片,base64 互转
阅读量:4676 次
发布时间:2019-06-09

本文共 1531 字,大约阅读时间需要 5 分钟。

import sun.misc.BASE64Decoder;import java.io.FileOutputStream;import java.io.OutputStream;/** * @author ydy * @version 2017/12/29  14:02 */public class ImageUtils {    /**		 *"data:image/jpeg;base64," 解码之前这个得去掉。     * 将base64编码字符串转换为图片     * @param imgStr base64编码字符串     * @param path 图片路径 - 具体到文件,即自行定义文件名     * @return     */    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);}		}

  

转载于:https://www.cnblogs.com/yccmelody/p/8144665.html

你可能感兴趣的文章
Nginx 常用命令总结
查看>>
hall wrong behavior
查看>>
Collection集合
查看>>
【C++】const在不同位置修饰指针变量
查看>>
github新项目挂历模式
查看>>
编写jquery插件
查看>>
敏捷开发笔记
查看>>
学前班
查看>>
关于自关联1
查看>>
hdu-1814(2-sat)
查看>>
谷歌浏览器,添加默认搜索引擎的搜索地址
查看>>
数据结构化与保存
查看>>
为什么需要Docker?
查看>>
国内5家云服务厂商 HTTPS 安全性测试横向对比
查看>>
how to control project
查看>>
转 python新手容易犯的6个错误
查看>>
第四节 -- 列表
查看>>
决策树
查看>>
团队作业
查看>>
如何避免在简单业务逻辑上面的细节上面出错
查看>>