package?cn.sanishan.util;??
import?java.io.FileInputStream;??
import?java.io.FileOutputStream;??
import?java.io.IOException;??
import?java.io.InputStream;??
import?java.io.OutputStream;??
import?java.net.HttpURLConnection;??
import?java.net.URL;??
import?sun.misc.BASE64Decoder;??
import?sun.misc.BASE64Encoder;??
/**
?*?
?*?版權(quán)所有:2016?項目名稱:ImgeBase64
?*
?*?類描述:將圖片轉(zhuǎn)化為Base64字符串?
?*?類名稱:cn.sanishan.util.Base64Img?
?*?創(chuàng)建人:
?*?創(chuàng)建時間:2016年10月27日
?*?下午3:25:49?
?*?修改人:?
?*?修改時間:2016年10月27日?下午3:25:49?
?*?修改備注:
?*?
?*?@version?V1.0
?*/??
public?class?Base64Img?{??
/**
?????*?@Title:?GetImageStrFromUrl
?????*?@Description:?TODO(將一張網(wǎng)絡(luò)圖片轉(zhuǎn)化成Base64字符串)
?????*?@param?imgURL?網(wǎng)絡(luò)資源位置
?????*?@return?Base64字符串
?????*/??
public?static?String?GetImageStrFromUrl(String?imgURL)?{??
byte[]?data?=?null;??
try?{??
//?創(chuàng)建URL??
URL?url?=new?URL(imgURL);??
//?創(chuàng)建鏈接??
????????????HttpURLConnection?conn?=?(HttpURLConnection)?url.openConnection();??
conn.setRequestMethod("GET");??
conn.setConnectTimeout(5?*?1000);??
????????????InputStream?inStream?=?conn.getInputStream();??
data?=new?byte[inStream.available()];??
????????????inStream.read(data);??
????????????inStream.close();??
}catch?(IOException?e)?{??
????????????e.printStackTrace();??
????????}??
//?對字節(jié)數(shù)組Base64編碼??
BASE64Encoder?encoder?=new?BASE64Encoder();??
//?返回Base64編碼過的字節(jié)數(shù)組字符串??
return?encoder.encode(data);??
????}??
/**
?????*?@Title:?GetImageStrFromPath
?????*?@Description:?TODO(將一張本地圖片轉(zhuǎn)化成Base64字符串)
?????*?@param?imgPath
?????*?@return
?????*/??
public?static?String?GetImageStrFromPath(String?imgPath)?{??
InputStream?in?=null;??
byte[]?data?=?null;??
//?讀取圖片字節(jié)數(shù)組??
try?{??
in?=new?FileInputStream(imgPath);??
data?=readInputStream(inStream);
????????????in.read(data);??
????????????in.close();??
}catch?(IOException?e)?{??
????????????e.printStackTrace();??
????????}??
//?對字節(jié)數(shù)組Base64編碼??
BASE64Encoder?encoder?=new?BASE64Encoder();??
//?返回Base64編碼過的字節(jié)數(shù)組字符串??
return?encoder.encode(data);??
????}??
/**
?????*?@Title:?GenerateImage
?????*?@Description:?TODO(base64字符串轉(zhuǎn)化成圖片)
?????*?@param?imgStr
?????*?@return
?????*/??
public?static?boolean?GenerateImage(String?imgStr)?{??
if?(imgStr?==?null)?//?圖像數(shù)據(jù)為空??
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)?{//?調(diào)整異常數(shù)據(jù)??
b[i]?+=256;??
????????????????}??
????????????}??
//?生成jpeg圖片??
String?imgFilePath?="d://222.jpg";??
OutputStream?out?=new?FileOutputStream(imgFilePath);??
????????????out.write(b);??
????????????out.flush();??
????????????out.close();??
return?true;??
}catch?(Exception?e)?{??
return?false;??
????????}??
????}??
}??
public static byte[] readInputStream(InputStream inStream) throws Exception{
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while( (len=inStream.read(buffer)) != -1 ){
outStream.write(buffer, 0, len);
}
inStream.close();
return outStream.toByteArray();
}