图片数据Base64编解码(java实现)


package com.tianmushanlu.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
/**
* 参考至博客 http://www.cnblogs.com/phinecos/archive/2008/09/23/1297292.html
*
*
*/
public class Base64Util {

/**
* 测试main方法
* @param args
*/
public static void main(String[] args) {
String imageStr = getImageStr("D:\\image.jpg");
System.out.println(imageStr);
generateImage(imageStr,"generateImage.jpg");
}

private static String LOCAL_FILE_PATH;
static {
LOCAL_FILE_PATH = System.getProperty("user.home");
// 如果不是以文件分隔符结尾,就补充文件分隔符
if (!LOCAL_FILE_PATH.endsWith(File.separator)) {
LOCAL_FILE_PATH += File.separator;
}
LOCAL_FILE_PATH += "conf" + File.separator + "image" + File.separator;
}

/**
* 生成字节数组字符串
*
* @param imgBase64Str
* 经过Base64编码过的字节数组字符串
* @return 字节数组字符串
*/
public static byte[] generateByte(String imgBase64Str) {
byte[] b = new byte[3072];
if (imgBase64Str == null) {
return b;
}

BASE64Decoder decoder = new BASE64Decoder();
try {
// Base64解码
b = decoder.decodeBuffer(imgBase64Str);
for (int i = 0; i < b.length; ++i) {
if (b[i] < 0) {// 调整异常数据
b[i] += 256;
}
}
return b;
} catch (Exception e) {
return b;
}
}

/**
* 将图片文件转化为字节数组字符串,并对其进行Base64编码处理
*
* @param imgContent
* 待处理的图片
* @return 返回Base64编码过的字节数组字符串
*/
public static String getImageStr(String imgContent) {
String imgBase64Str = null;
InputStream in = null;
byte[] data = null;
if( imgContent == null || ("").equals(imgContent) ) {
return imgBase64Str;
}
// 读取图片字节数组
try {
in = new FileInputStream(imgContent);
data = new byte[in.available()];
in.read(data);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
// 对字节数组Base64编码
BASE64Encoder encoder = new BASE64Encoder();
imgBase64Str = encoder.encode(data);
return imgBase64Str;
}

/**
* 对字节数组字符串进行Base64解码并生成图片
*
* @param imgBase64Str
* 经过Base64编码过的字节数组字符串
* @param imgName
* 图片名
* @return 是否生成图片
*/
public static boolean generateImage(String imgBase64Str, String imgName) {
OutputStream out = null;
if (imgName == null || ("").equals(imgName)) {
return false;
}

if (imgBase64Str == null || ("").equals(imgBase64Str)) {
return false;
}

BASE64Decoder decoder = new BASE64Decoder();
try {
// Base64解码
byte[] b = decoder.decodeBuffer(imgBase64Str);
for (int i = 0; i < b.length; ++i) {
if (b[i] < 0) {// 调整异常数据
b[i] += 256;
}
}
/**
* 生成图片文件,存储路径为C:\Users\xxx(你的主机名)\conf\temp
*
*/
//如果目录不存在,创建目录
File localImageFolder = new File(LOCAL_FILE_PATH);
if(!localImageFolder.exists()) {
localImageFolder.mkdirs();
}
//如果目录不为空清空目录,因为都是单线程操作,这里清除不会相互影响
File[] subFiles = localImageFolder.listFiles();
if(null != subFiles) {
for(File subFile : subFiles) {
subFile.delete();
}
}

out = new FileOutputStream(LOCAL_FILE_PATH + imgName);
out.write(b);
out.flush();
return true;
} catch (Exception e) {
return false;
} finally {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值