首页 技术 正文
技术 2022年11月21日
0 收藏 759 点赞 3,108 浏览 3287 个字

一、将本地图片转换成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;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,031
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,520
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,368
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,148
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,781
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,860