一、将本地图片转换成Base64编码字符串

/**
* 将本地图片转换成Base64编码字符串
*
* @param imgFile 图片目录路径
* @return
*/
public static String getImgFileToBase64(String imgFile) {
//将图片文件转化为字节数组字符串,并对其进行Base64编码处理
InputStream inputStream = null;
byte[] buffer = null;
//读取图片字节数组
try {
inputStream = new FileInputStream(imgFile);
int count = 0;
while (count == 0) {
count = inputStream.available();
}
buffer = new byte[count];
inputStream.read(buffer);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (inputStream != null) {
try {
// 关闭inputStream流
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
// 对字节数组Base64编码
return new BASE64Encoder().encode(buffer);
}

二、将网络图片转换成Base64编码字符串

/**
* 将网络图片转换成Base64编码字符串
*
* @param imgUrl 网络图片Url
* @return
*/
public static String getImgUrlToBase64(String imgUrl) {
InputStream inputStream = null;
ByteArrayOutputStream outputStream = null;
byte[] buffer = null;
try {
// 创建URL
URL url = new URL(imgUrl);
// 创建链接
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5000);
inputStream = conn.getInputStream();
outputStream = new ByteArrayOutputStream();
// 将内容读取内存中
buffer = new byte[1024];
int len = -1;
while ((len = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, len);
}
buffer = outputStream.toByteArray();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (inputStream != null) {
try {
// 关闭inputStream流
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (outputStream != null) {
try {
// 关闭outputStream流
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
// 对字节数组Base64编码
return new BASE64Encoder().encode(buffer);
}

三、将网络链接图片或者本地图片文件转换成Base64编码字符串(就是对上面的两种进行整合)

/**
* 将网络链接图片或者本地图片文件转换成Base64编码字符串
*
* @param imgStr 网络图片Url/本地图片目录路径
* @return
*/
public static String getImgStrToBase64(String imgStr) {
InputStream inputStream = null;
ByteArrayOutputStream outputStream = null;
byte[] buffer = null;
try {
//判断网络链接图片文件/本地目录图片文件
if (imgStr.startsWith("http://") || imgStr.startsWith("https://")) {
// 创建URL
URL url = new URL(imgStr);
// 创建链接
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5000);
inputStream = conn.getInputStream();
outputStream = new ByteArrayOutputStream();
// 将内容读取内存中
buffer = new byte[1024];
int len = -1;
while ((len = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, len);
}
buffer = outputStream.toByteArray();
} else {
inputStream = new FileInputStream(imgStr);
int count = 0;
while (count == 0) {
count = inputStream.available();
}
buffer = new byte[count];
inputStream.read(buffer);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (inputStream != null) {
try {
// 关闭inputStream流
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (outputStream != null) {
try {
// 关闭outputStream流
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
// 对字节数组Base64编码
return new BASE64Encoder().encode(buffer);
}

四、将图片Base64编码转换成img图片文件(解码)

/**
* 将图片Base64编码转换成img图片文件
*
* @param imgBase64 图片Base64编码
* @param imgPath 图片生成路径
* @return
*/
public static boolean getImgBase64ToImgFile(String imgBase64, String imgPath) {
boolean flag = true;
OutputStream outputStream = null;
try {
// 解密处理数据
byte[] bytes = new BASE64Decoder().decodeBuffer(imgBase64);
for (int i = 0; i < bytes.length; ++i) {
if (bytes[i] < 0) {
bytes[i] += 256;
}
}
outputStream = new FileOutputStream(imgPath);
outputStream.write(bytes);
} catch (Exception e) {
e.printStackTrace();
flag = false;
} finally {
if (outputStream != null) {
try {
// 关闭outputStream流
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return flag;
}

Java对网络图片/本地图片转换成Base64编码和解码的更多相关文章

  1. delphi将图片转换成Base64编码函数

    {************************************************************************** 名称: BaseImage 参数: fn: TF ...

  2. 利用PHP将图片转换成base64编码的实现方法

    先来说一下为什么我们要对图片base64编码 base64是当前网络上最为常见的传输8Bit字节代码的编码方式其中之一.base64主要不是加密,它主要的用途是把某些二进制数转成普通字符用于网络传输. ...

  3. 图片转换成Base64编码集成到html文件

    首先为什么要这么做?  原因很简单这样可以减少与服务器的请求,当然对于一些浏览器并不支持,如IE8.通常用在手机版网站中,具体转化方法如下: 1.在线打开Base64的编码器将图片编码成Base64 ...

  4. java 图片转换成base64字符串

    import java.io.ByteArrayOutputStream; import java.io.FileInputStream;import java.io.FileOutputStream ...

  5. 网络图片转换到本地并转换成base64位

    /** * 网络图片转换到本地并转换成base64位 * @param $url * @return string */ public function imgzhuanhuan($url) { // ...

  6. Base64字符保存图片,图片转换成Base64字符编码

    //文件转换成Base64编码 public static String getFileBase64Str(String filePath) throws IOException { String f ...

  7. js绝对地址图片转换成base64的方法

    //将图片转换成base64 function getBase64Image(url, callback){ var canvas = document.createElement('canvas') ...

  8. java中如何把图片转换成二进制流的代码

    在学习期间,把开发过程经常用到的一些代码段做个备份,下边代码内容是关于java中如何把图片转换成二进制流的代码,应该能对各朋友也有用处. public byte[] SetImageToByteArr ...

  9. JS将图片转换成Base64码

    直接上代码 html页面代码 <!DOCTYPE html> <html lang="en"> <head> <meta charset= ...

随机推荐

  1. Neutron的安全组原理

    ​ Security group通过Linux IPtables来实现,为此,在Compute节点上引入了qbr*这样的Linux传统bridge(iptables规则目前无法加载到直接挂在到ovs的 ...

  2. sql语言分类与整理:DQL\DML\DDL

    整体分为三类: 数据库查询语言(DQL,data QUERY LANGUAGE):对表的查询语句,select 数据库定义语言(DDL,data defined LANGUAGE):create da ...

  3. canvas操作图片,进行面板画图,旋转等

    HTML5 <canvas> 标签用于绘制图像(通过脚本,通常是 JavaScript). 不过,<canvas> 元素本身并没有绘制能力(它仅仅是图形的容器) - 您必须使用 ...

  4. Agilent RF fundamentals (11)-Vector modulator

     Vector modulator 矢量调制器:调整信号的幅度和相位 http://www.21ic.com/app/test/201805/762401.htm

  5. php调用API支付接口(转自刘68)

    首先访问  https://charging.teegon.com/  注册账号, 找到开发配置   记下client_id和client_secret. 点击 天工开放平台 点击天工收银 点击  S ...

  6. phpcms ——模板标签使用说明

    使用phpcms总是要查询各种标签,实在很烦,只好找个比较全的来备查.因为自己写一个orm来配合调用也没那么容易无缝的嵌入到引擎当中. 获取父分类下面的子分类 {loop subcat(77) $k ...

  7. Android 进阶7:进程通信之 AIDL 的使用

    读完本文你将了解: AIDL 是什么 AIDL 支持的数据类型 AIDL 如何编写 AIDL 实例 创建 AIDL 编写服务端代码 编写客户端代码 运行结果 总结 代码地址 Thanks 记得 201 ...

  8. c#和c++互操作(平台调用相关)

    [DllImport("ScreenCaptureLib.dll", CallingConvention = CallingConvention.Cdecl)] public st ...

  9. ACM学习历程—计蒜客15 单独的数字(位运算)

    http://nanti.jisuanke.com/t/15 题目要求是求出只出现一次的数字,其余数字均出现三次. 之前有过一个题是其余数字出现两次,那么就是全部亦或起来就得到答案. 这题有些不太一样 ...

  10. sublime text 3设置浏览器快捷键

    一.设置默认浏览器 1,打开sublime 依次选择 tools > build system > new build system... 2,选择你喜欢的浏览器,右键 > 属性 把 ...