Java中Base64的幾種用法小結(jié)
一、簡(jiǎn)要概述
在 Java 中使用 Base64 編碼和解碼非常常見,尤其是在處理網(wǎng)絡(luò)傳輸、加密數(shù)據(jù)、圖片處理等場(chǎng)景。Java 8 及以上版本提供了內(nèi)置的 java.util.Base64 工具類,非常方便使用。
二、幾種用法
1、字符串編碼解碼
/**
* URL安全編碼
*/
@Test
public void test1()
{
String input = RandomStringUtils.randomAlphanumeric(50);
String encoded = Base64.getUrlEncoder().encodeToString(input.getBytes());
log.info("{} --> {}", input, encoded);
// 解碼
byte[] decodeBytes = Base64.getUrlDecoder().decode(encoded);
log.info(new String(decodeBytes));
}
2、文件編碼解碼
/**
* 安全編碼文件
*
* @throws IOException
*/
@Test
public void testFile()
throws IOException
{
byte[] fileBytes = Files.readAllBytes(Paths.get(new ClassPathResource("log4j2.xml").getURI()));
String encoded = Base64.getUrlEncoder().encodeToString(fileBytes);
log.info("Encoded file: {}", encoded);
// 解碼
byte[] decodeBytes = Base64.getUrlDecoder().decode(encoded);
log.info(new String(decodeBytes));
}
3、本地圖片編碼解碼
/**
* 安全編碼圖片
*
* @throws IOException
*/
@Test
public void testPic()
throws IOException
{
// Paths相對(duì)路徑為當(dāng)前項(xiàng)目
byte[] fileBytes = Files.readAllBytes(Paths.get("src/main/resources/data/18.jpg"));
String encoded = Base64.getUrlEncoder().encodeToString(fileBytes);
log.info("Encoded file: {}", encoded);
// 解碼
byte[] decodeBytes = Base64.getUrlDecoder().decode(encoded);
Files.write(Paths.get("/001.jpg"), decodeBytes);
}
4、網(wǎng)絡(luò)圖片編碼解碼
/**
* 安全編碼圖片
*
* @throws IOException
*/
@Test
public void testNetPic()
throws IOException
{
Resource resource = new UrlResource("https://i-blog.csdnimg.cn/direct/d4f0934d9ac041d583f81126fcc40f1d.png");
try (InputStream is = resource.getInputStream())
{
// 編碼解碼網(wǎng)絡(luò)圖片
String encoded = Base64.getUrlEncoder().encodeToString(IOUtils.toByteArray(is));
byte[] decodeBytes = Base64.getUrlDecoder().decode(encoded);
Files.write(Paths.get("/002.jpg"), decodeBytes);
}
}
到此這篇關(guān)于Java中Base64的幾種用法小結(jié)的文章就介紹到這了,更多相關(guān)Java Base64用法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
springboot整合sentinel接口熔斷的實(shí)現(xiàn)示例
為了防止慢接口導(dǎo)致的服務(wù)阻塞,可以通過添加熔斷處理來避免應(yīng)用的大量工作線程陷入阻塞,保證其他接口的正常運(yùn)行,本文介紹了如何使用Spring Boot與Sentinel進(jìn)行接口熔斷的配置與實(shí)現(xiàn),感興趣的可以了解一下2024-09-09
JavaSE中compare、compareTo的區(qū)別
本文主要介紹了JavaSE中compare、compareTo的區(qū)別,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05
SSH框架實(shí)現(xiàn)表單上傳圖片實(shí)例代碼
本篇文章主要介紹了SSH框架實(shí)現(xiàn)表單上傳圖片實(shí)例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-09-09
Java內(nèi)部類持有外部類導(dǎo)致內(nèi)存泄露的原因與解決方案詳解
這篇文章主要為大家詳細(xì)介紹了Java因?yàn)閮?nèi)部類持有外部類導(dǎo)致內(nèi)存泄露的原因以及其解決方案,文中的示例代碼講解詳細(xì),希望對(duì)大家有所幫助2022-11-11
SpringBoot中yml多環(huán)境配置的3種方法
這篇文章主要給大家介紹了SpringBoot中yml多環(huán)境配置的3種方法,文中有詳細(xì)的代碼示例供大家參考,對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2023-10-10
Java中的遞歸詳解(用遞歸實(shí)現(xiàn)99乘法表來講解)
這篇文章主要介紹了Java中的遞歸詳解(用遞歸實(shí)現(xiàn)99乘法表來講解),本文給出了普通的99乘法實(shí)現(xiàn)方法和用遞歸實(shí)現(xiàn)的方法,并對(duì)比它們的不同,體現(xiàn)出遞歸的運(yùn)用及理解,需要的朋友可以參考下2015-03-03
解析電子郵件的基本概念及JavaMail API郵件功能使用
這篇文章主要介紹了電子郵件的基本概念及JavaMail API郵件功能使用,包括用Java來發(fā)送郵件的示例,需要的朋友可以參考下2016-02-02

