Java中將base64編碼字符串轉(zhuǎn)換為圖片的代碼
前一段時間,在做攝像頭拍照上傳,攝像頭拍的照片為base64編碼格式的字符串,需要上傳至項目中,則需要使用到將base64編碼字符串轉(zhuǎn)換為圖片
1、將base64編碼字符串轉(zhuǎn)換為圖片的代碼如下 ImageUtil.java:
package util;
import javax.servlet.http.HttpServletRequest;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Base64;
import java.util.UUID;
public class ImageUtil {
/**
* @Description: 將base64編碼字符串轉(zhuǎn)換為圖片
* @Author:
* @CreateTime:
* @param file base64編碼字符串
* @param path 圖片路徑-具體到文件
* @return
*/
public static String generateImage(String file, String path, HttpServletRequest request) {
// 解密
try {
// 項目絕對路徑
String savePath = request.getSession().getServletContext().getRealPath("upload");
// 圖片分類路徑+圖片名+圖片后綴
String imgClassPath = path.concat(UUID.randomUUID().toString()).concat(".jpg");
// 解密
Base64.Decoder decoder = Base64.getDecoder();
// 去掉base64前綴 data:image/jpeg;base64,
file = file.substring(file.indexOf(",", 1) + 1, file.length());
byte[] b = decoder.decode(file);
// 處理數(shù)據(jù)
for (int i = 0; i < b.length; ++i) {
if (b[i] < 0) {
b[i] += 256;
}
}
// 保存圖片
OutputStream out = new FileOutputStream(savePath.concat(imgClassPath));
out.write(b);
out.flush();
out.close();
// 返回圖片的相對路徑 = 圖片分類路徑+圖片名+圖片后綴
return imgClassPath;
} catch (IOException e) {
return null;
}
}
}
補充:Java實現(xiàn)圖片轉(zhuǎn)base64字符串和圖片互相轉(zhuǎn)換
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import java.io.*;
/**
* @Description:
* @Author: Han
* @CreateDate: 2022/9/7
**/
public class Test010 {
public static void main(String[] args) {
String base64Str = imageToBase64Str("D:\\SoftWare\\圖片素材\\頭像\\432.jpeg");
System.out.println(base64Str);
boolean b = base64StrToImage(base64Str, "D:\\002.jpg");
System.out.println(b);
}
/**
* 圖片轉(zhuǎn)base64字符串
*
* @param imgFile 圖片路徑
* @return
*/
public static String imageToBase64Str(String imgFile) {
InputStream inputStream = null;
byte[] data = null;
try {
inputStream = new FileInputStream(imgFile);
data = new byte[inputStream.available()];
inputStream.read(data);
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
// 加密
BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(data);
}
/**
* base64編碼字符串轉(zhuǎn)換為圖片,并寫入文件
*
* @param imgStr base64編碼字符串
* @param path 圖片路徑
* @return
*/
public static boolean base64StrToImage(String imgStr, String path) {
if (imgStr == null)
return false;
BASE64Decoder decoder = new BASE64Decoder();
try {
// 解密
byte[] b = decoder.decodeBuffer(imgStr);
// 處理數(shù)據(jù)
for (int i = 0; i < b.length; ++i) {
if (b[i] < 0) {
b[i] += 256;
}
}
//文件夾不存在則自動創(chuàng)建
File tempFile = new File(path);
if (!tempFile.getParentFile().exists()) {
tempFile.getParentFile().mkdirs();
}
OutputStream out = new FileOutputStream(tempFile);
out.write(b);
out.flush();
out.close();
return true;
} catch (Exception e) {
return false;
}
}
}
到此這篇關(guān)于Java中將base64編碼字符串轉(zhuǎn)換為圖片的文章就介紹到這了,更多相關(guān)Java base64編碼字符串轉(zhuǎn)換為圖片內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java抽象類和普通類區(qū)別、 數(shù)組跟List的區(qū)別解析
這篇文章主要介紹了Java抽象類和普通類區(qū)別、 數(shù)組跟List的區(qū)別,在這里需要注意List是一個接口,不能直接實例化,需要使用具體的實現(xiàn)類來創(chuàng)建對象,本文結(jié)合示例代碼介紹的非常詳細(xì),需要的朋友參考下吧2023-09-09
IntelliJ IDEA 好用插件之a(chǎn)nalyze inspect code詳解
這篇文章主要介紹了IntelliJ IDEA 好用插件之a(chǎn)nalyze inspect code的相關(guān)知識,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2020-12-12
Java并發(fā)多線程編程之CountDownLatch的用法
這篇文章主要介紹了Java并發(fā)多線程編程之CountDownLatch的用法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06
解決Springboot配置excludePathPatterns不生效的問題
這篇文章主要介紹了解決Springboot配置excludePathPatterns不生效的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-10-10
Spring中的singleton和prototype的實現(xiàn)
這篇文章主要介紹了Spring中的singleton和prototype的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07

