如下分享照片与BASE64字符串转换工具类:
package com.tecsun.sisp.iface.common.util
;
import sun.misc.BASE64Decoder
;
import sun.misc.BASE64Encoder
;
import java.io.*
;
/**
* Created by Sandwich on 2015/11/12.
*/
public class PicUtil {
/**
* 照片转化成base64字符串 path为照片路径
* @param path 照片路径
* @return
*/
public static String
GetImageStr(String path) {
String imgFile = path
;// 待处理的voice
InputStream in =
null;
byte[] data =
null;
/**读取照片字节数组**/
try {
in =
new FileInputStream(imgFile)
;
data =
new byte[in.available()]
;
in.read(data)
;
in.close()
;
}
catch (IOException e) {
e.printStackTrace()
;
}
/** 对字节数组Base64编码**/
BASE64Encoder encoder =
new BASE64Encoder()
;
/** 返回Base64编码过的字节数组字符串**/
return encoder.encode(data)
;
}
/**
* 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 {
// Base64解码
byte[] b = decoder.decodeBuffer(imgStr)
;
for (
int i =
0; i < b.
length; ++i) {
if (b[i] <
0) {
// 调整异常数据
b[i] +=
256;
}
}
// 生成照片
String imgFilePath = path
;// 新生成的照片
OutputStream out =
new FileOutputStream(imgFilePath)
;
out.write(b)
;
out.flush()
;
out.close()
;
return true;
}
catch (Exception e) {
return false;
}
}
}
原创链接:
http://blog.csdn.net/zetting_/article/details/53214422
转载请注明原文地址: https://ju.6miu.com/read-678725.html