需求是把服務(wù)器的圖片鏈接或者網(wǎng)上的圖片鏈接地址轉(zhuǎn)為base64位編碼方便前端操作文章來源:http://www.zghlxwxcb.cn/news/detail-522288.html
建議使用方法一
base64編碼轉(zhuǎn)為圖片在線網(wǎng)址
https://imgtobase64.d777.com/文章來源地址http://www.zghlxwxcb.cn/news/detail-522288.html
方法一:使用hutool的HttpResponse方法
1.1 引入依賴
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.7.22</version>
</dependency>
1.2 代碼
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import sun.misc.BASE64Encoder;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @Author ekkcole
* @remark 圖片鏈接轉(zhuǎn)為base64編碼
*/
public class UrlToBase64Util {
//base64前綴
private static final String BASE64_PREFIX="data:image/png;base64,";
public static void main(String[] args) throws Exception {
String url="https://localhost:8080/upload/file/20221101/test.png";
System.out.println(BASE64_PREFIX+imageUrlToBase64(url));
}
/**
* 圖片URL轉(zhuǎn)Base64編碼
*
* @param imgUrl 圖片URL
* @return Base64編碼
*/
public static String imageUrlToBase64(String imgUrl) {
InputStream is = null;
ByteArrayOutputStream outStream = null;
try {
if (!ObjectUtils.isEmpty(imgUrl)) {
HttpResponse res = HttpRequest.get(imgUrl).execute();
// 獲取輸入流
is = res.bodyStream();
outStream = new ByteArrayOutputStream();
//創(chuàng)建一個Buffer字符串
byte[] buffer = new byte[1024];
//每次讀取的字符串長度,如果為-1,代表全部讀取完畢
int len = 0;
//使用輸入流從buffer里把數(shù)據(jù)讀取出來
while ((len = is.read(buffer)) != -1) {
//用輸出流往buffer里寫入數(shù)據(jù),中間參數(shù)代表從哪個位置開始讀,len代表讀取的長度
outStream.write(buffer, 0, len);
}
// 對字節(jié)數(shù)組Base64編碼
return encode(outStream.toByteArray());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (is != null) {
is.close();
}
if (outStream != null) {
outStream.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
/**
* 圖片轉(zhuǎn)字符串
*
* @param image 圖片Buffer
* @return Base64編碼
*/
public static String encode(byte[] image) {
BASE64Encoder decoder = new BASE64Encoder();
return replaceEnter(decoder.encode(image));
}
/**
* 字符替換
*
* @param str 字符串
* @return 替換后的字符串
*/
public static String replaceEnter(String str) {
String reg = "[\n-\r]";
Pattern p = Pattern.compile(reg);
Matcher m = p.matcher(str);
return m.replaceAll("");
}
}
方法二:使用自帶的請求,有的網(wǎng)址會報錯,建議用第一種
import org.springframework.util.ObjectUtils;
import sun.misc.BASE64Encoder;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class UrlToBase64 {
//base64前綴
private static final String BASE64_PREFIX="data:image/png;base64,";
public static void main(String[] args) throws Exception {
String url="https://localhost:8080/upload/file/20221101/test.png";
System.out.println(BASE64_PREFIX+imageUrlToBase64(url));
}
/**
* 圖片URL轉(zhuǎn)Base64編碼
* @param imgUrl 圖片URL
* @return Base64編碼
*/
public static String imageUrlToBase64(String imgUrl) {
URL url = null;
InputStream is = null;
ByteArrayOutputStream outStream = null;
HttpURLConnection httpUrl = null;
try {
if (!ObjectUtils.isEmpty(imgUrl)) {
// 如果服務(wù)器圖片地址帶有中文,最好處理一下
String[] argss = imgUrl.split("/");
String name1 = argss[argss.length - 1];
String name2 = java.net.URLEncoder.encode(name1, "utf-8");
// 處理中文名
String newUrl = imgUrl.replace(name1, name2);
url = new URL(newUrl);
httpUrl = (HttpURLConnection) url.openConnection();
httpUrl.connect();
httpUrl.getInputStream();
is = httpUrl.getInputStream();
outStream = new ByteArrayOutputStream();
//創(chuàng)建一個Buffer字符串
byte[] buffer = new byte[1024];
//每次讀取的字符串長度,如果為-1,代表全部讀取完畢
int len = 0;
//使用輸入流從buffer里把數(shù)據(jù)讀取出來
while ((len = is.read(buffer)) != -1) {
//用輸出流往buffer里寫入數(shù)據(jù),中間參數(shù)代表從哪個位置開始讀,len代表讀取的長度
outStream.write(buffer, 0, len);
}
// 對字節(jié)數(shù)組Base64編碼
return encode(outStream.toByteArray());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if(is != null) {
is.close();
}
if(outStream != null) {
outStream.close();
}
if(httpUrl != null) {
httpUrl.disconnect();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
/**
* 圖片轉(zhuǎn)字符串
* @param image 圖片Buffer
* @return Base64編碼
*/
public static String encode(byte[] image){
BASE64Encoder decoder = new BASE64Encoder();
return replaceEnter(decoder.encode(image));
}
/**
* 字符替換
* @param str 字符串
* @return 替換后的字符串
*/
public static String replaceEnter(String str){
String reg ="[\n-\r]";
Pattern p = Pattern.compile(reg);
Matcher m = p.matcher(str);
return m.replaceAll("");
}
}
到了這里,關(guān)于java使用hutool把服務(wù)器圖片鏈接轉(zhuǎn)為base64編碼的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!