本篇文章為大家展示了利用java怎么在后臺(tái)將base64字符串保存為圖片,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過(guò)這篇文章的詳細(xì)介紹希望你能有所收獲。
具體方法如下:
import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; public class Base64Test { public static void main(String[] args) { String strImg = GetImageStr(); System.out.println(strImg); GenerateImage(strImg); } //圖片轉(zhuǎn)化成base64字符串 public static String GetImageStr() {//將圖片文件轉(zhuǎn)化為字節(jié)數(shù)組字符串,并對(duì)其進(jìn)行Base64編碼處理 String imgFile = "D:\\tupian\\a.jpg";//待處理的圖片 InputStream in = null; byte[] data = null; //讀取圖片字節(jié)數(shù)組 try { in = new FileInputStream(imgFile); data = new byte[in.available()]; in.read(data); in.close(); } catch (IOException e) { e.printStackTrace(); } //對(duì)字節(jié)數(shù)組Base64編碼 BASE64Encoder encoder = new BASE64Encoder(); return encoder.encode(data);//返回Base64編碼過(guò)的字節(jié)數(shù)組字符串 } //base64字符串轉(zhuǎn)化成圖片 public static boolean GenerateImage(String imgStr) { //對(duì)字節(jié)數(shù)組字符串進(jìn)行Base64解碼并生成圖片 if (imgStr == null) //圖像數(shù)據(jù)為空 return false; BASE64Decoder decoder = new BASE64Decoder(); try { //Base64解碼 byte[] b = decoder.decodeBuffer(imgStr); for(int i=0;i
網(wǎng)站標(biāo)題:利用java怎么在后臺(tái)將base64字符串保存為圖片-創(chuàng)新互聯(lián)
文章網(wǎng)址:http://weahome.cn/article/doohoi.html