JAVA實現網絡/本地圖片轉BASE64存儲代碼示例
更新時間:2023年07月28日 11:21:46 作者:小碼農~LR
這篇文章主要給大家介紹了關于JAVA實現網絡/本地圖片轉BASE64存儲的相關資料,Base64是網絡上最常見的用于傳輸8Bit字節(jié)碼的編碼方式之一,Base64就是一種基于64個可打印字符來表示二進制數據的方法,需要的朋友可以參考下
網絡圖片轉BASE64
String encoder = "data:image/jpg;base64,"; //定義圖片類型,方便前端直接使用
ByteArrayOutputStream data = new ByteArrayOutputStream();
URL url = new URL(picUrl);//picUrl為圖片地址
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
InputStream is = connection.getInputStream();
byte[] bytes = new byte[1024];
int len = 0;
while ((len = is.read(bytes)) != -1){
data.write(bytes,0,len);
}
is.close();
BASE64Encoder base64Encoder = new BASE64Encoder();
encoder = encoder + base64Encoder.encode(data.toByteArray()).replace("\r\n","").trim();//這里去掉結果里面的"\r\n",也可以不去,但是不去的話需要使用的時候還是要去掉,所以為了方便就先去掉再存儲如果是本地圖片的話,其實和網絡圖片相差不多的,主要就是讀取圖片流的形式變一下
String encoder = "data:image/jpg;base64,"; //定義圖片類型,方便前端直接使用
ByteArrayOutputStream data = new ByteArrayOutputStream();
String filePath = "filePath";//這里的filePath為本地存放圖片的地址
FileInputStream is = new FileInputStream(filePath);
byte[] bytes = new byte[1024];
int len = 0;
while ((len = is.read(bytes)) != -1){
data.write(bytes,0,len);
}
is.close();
BASE64Encoder base64Encoder = new BASE64Encoder();
encoder = encoder + base64Encoder.encode(data.toByteArray()).replace("\r\n","").trim();//這里去掉結果里面的"\r\n",也可以不去,但是不去的話需要使用的時候還是要去掉,所以為了方便就先去掉再存儲附:Java實現圖片和base64之間的互轉
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.Base64;
public class ImageUtil {
/**
* 圖片轉Base64碼
* @param src
* @return
*/
public static String convertImageToBase64Str(String src) {
ByteArrayOutputStream baos = null;
try {
String suffix = src.substring(src.lastIndexOf(".") + 1);
File imageFile = new File(src);
BufferedImage bufferedImage = ImageIO.read(imageFile);
baos = new ByteArrayOutputStream();
ImageIO.write(bufferedImage, suffix, baos);
byte[] bytes = baos.toByteArray();
return Base64.getEncoder().encodeToString(bytes);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (baos != null) {
baos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
/**
* Base64碼轉圖片
* @param base64String
* @param newSrc
*/
public static void convertBase64StrToImage(String base64String, String newSrc) {
ByteArrayInputStream bais = null;
try {
String suffix = newSrc.substring(newSrc.lastIndexOf(".") + 1);
byte[] bytes = Base64.getDecoder().decode(base64String);
bais = new ByteArrayInputStream(bytes);
BufferedImage bufferedImage = ImageIO.read(bais);
File imageFile = new File(newSrc);
ImageIO.write(bufferedImage, suffix, imageFile);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (bais != null) {
bais.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}總結
到此這篇關于JAVA實現網絡/本地圖片轉BASE64存儲的文章就介紹到這了,更多相關JAVA圖片轉BASE64存儲內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Spring?Data?JPA?注解Entity關聯(lián)關系使用詳解
這篇文章主要為大家介紹了Spring?Data?JPA?注解Entity關聯(lián)關系使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-09-09
詳解Spring框架之基于Restful風格實現的SpringMVC
這篇文章主要介紹了詳解Spring框架之基于Restful風格實現的SpringMVC,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-05-05
如何使用Playwright對Java API實現自動視覺測試
這篇文章主要介紹了如何使用Playwright對Java API實現自動視覺測試,幫助大家更好的理解和使用Playwright,感興趣的朋友可以了解下2021-01-01

