在Java Base64 Decode中,Base64是一種二進位到文字格式的編碼方案,表示二進位數據,透過轉換為基數64描述,以可列印的ASCII字串格式的形式出現。這些Base64資料可以根據使用者的需求並藉助一定的方法進行編碼或解碼。為此,導入 java.util.Base64 套件是必不可少的一步。對這些資料進行編碼和解碼的主要優點是其隱私性和安全性。在以下部分中,將詳細描述每種方法。
開始您的免費軟體開發課程
網頁開發、程式語言、軟體測試及其他
聲明:
以下是Base64解碼的聲明:
public static class Base64.Decoder extends Object
Java 中 Base64 解碼如何運作?
現在,讓我們來看看 Base64 解碼的工作原理。
- 首先,建立一個編碼器對象,並根據需求對字串/位元組/位元組緩衝區進行編碼。可以使用 Base64.getEncoder() 方法來完成。
- 完成後,為編碼資料建立一個新數組。
- 要解碼數據,請建立一個解碼器對象,並建立另一個陣列來儲存解碼後的資料。
- 使用decode()方法解碼編碼資料。
- 列印解碼器陣列中可用的結果。
Java Base64 解碼方法
以下是Java Base64解碼的不同方法。
1.公共 by[] 解碼 ( byte[] arr )
- 借助 Base64 編碼方案解碼輸入數組中的每個位元組。
- 結果將寫入新分配的位元組數組中。
- 傳回的位元組數組的長度將與結果位元組相似。
- 這裡,arr 是需要解碼的輸入位元組數組。
- 如果輸入位元組陣列不是有效的 Base64 格式,將會拋出 IllegalArgumentException。
範例:
代碼:
import java.util.Base64; public class Base64DecodeExample { public static void main(String[] args) { // encoder Base64.Encoder enc = Base64.getEncoder(); // Encode byte array byte arr[] = {'a'}; byte arr2[] = enc.encode(arr); System.out.println("Array encoded is: "+ arr2); // decoder Base64.Decoder dec = Base64.getDecoder(); // Decode byte array String ds = new String(dec.decode(arr2)); System.out.println("Array decoded is:"+ds); } }
輸出:
執行程式碼時會列印數組的編碼和解碼結果。
2.公用位元組[]解碼(Stringstr)
- 輸入的 Base64 格式的字串將藉助 Base64 編碼方案進行解碼。
- 結果將寫入新分配的位元組數組中。
- 執行此方法將產生執行方法decode( src.getBytes ( StandardCharsets.ISO_8859_1 ) ) 的效果。
- 這裡,str是需要解碼的輸入字串。
- 如果輸入字串不是有效的 Base64 格式,將會拋出 IllegalArgumentException。
範例:
代碼:
import java.util.Base64; public class Base64DecodeExample { public static void main(String[] args) { // encoder Base64.Encoder enc = Base64.getEncoder(); String s = enc.encodeToString("EduCBA".getBytes()); System.out.println("String encoded is: "+ s); // decoder Base64.Decoder dec = Base64.getDecoder(); // Decode string String ds = new String(dec.decode(s)); System.out.println("String decoded is:"+ds); }}
輸出:
執行程式碼時會列印字串的編碼和解碼結果。
3. public int 解碼 ( byte[] arr, byte[] arr2)
- 借助 Base64 編碼方案解碼輸入數組中的每個位元組。
- 結果將寫入位元組數組arr2,從0開始偏移。
- 這裡,arr 是需要解碼的輸入位元組數組,arr2 是輸出數組。確保 arr2 有足夠的空間來容納解碼後的輸入位元組。
- 如果輸入位元組數組不是有效的 Base64 格式或 arr2 沒有足夠的空間來寫入解碼的輸入字節,則會拋出 IllegalArgumentException。
範例:
代碼:
import java.util.Base64; public class Base64DecodeExample { public static void main(String[] args) { // encoder Base64.Encoder enc = Base64.getEncoder(); byte arr[] = {'1'}; byte arr2[] = enc.encode(arr); byte arr3[] = new byte[5]; System.out.println("Array encoded is: "+ arr2); // decoder Base64.Decoder dec = Base64.getDecoder(); System.out.println("Array decoded is:"+ dec.decode(arr2,arr3)); }}
輸出:
執行程式碼時會列印位元組數組的編碼和解碼結果。
4.公 ByteBufferdecode ( ByteBufferbuff )
- 借助 Base64 編碼方案解碼輸入位元組緩衝區中的每個位元組。
- 結果將寫入新分配的位元組緩衝區。
- 這裡的buff是需要解碼的inputbytebuffer。
- 如果輸入位元組緩衝區不是有效的 Base64 格式,將會拋出 IllegalArgumentException。
範例:
代碼:
import java.nio.ByteBuffer; import java.util.Base64; public class Base64DecodeExample { public static void main(String[] args) { // encoder Base64.Encoder enc = Base64.getEncoder(); String st = "Happy weekend"; ByteBuffer buff= ByteBuffer.wrap(st.getBytes()); ByteBuffer buff2 = enc.encode(buff); System.out.print("Encoded: "); while(buff2.hasRemaining()){ char ch = (char) buff2.get(); System.out.print(ch); } buff2.clear(); // decoder Base64.Decoder dec = Base64.getDecoder(); ByteBuffer buff3 = dec.decode(buff2); System.out.print(" Decoded: "); while(buff3.hasRemaining()){ char ch3 = (char) buff3.get(); System.out.print(ch3); } buff2.clear(); } }
輸出:
執行程式碼時列印的位元組緩衝區的編碼和解碼結果。
5. public InputStreamwrap ( InputStreaminpt )
- An input stream will be returned in order to decode the byte stream, which is Base64 encoded.
- Here, input is the input stream.
Example
Code:
import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.Base64; public class Base64DecodeExample { public static void main(String[] args) throws IOException { try (InputStream inpt = new FileInputStream("F:\\EduCBA\\April\\Edu.txt")) { Base64.Encoder enc = Base64.getEncoder(); OutputStream opst = enc.wrap(new FileOutputStream("F:\\EduCBA\\April\\Eduout.txt")); int b1; while ((b1 = inpt.read()) != -1) { opst.write(b1); } opst.close(); } catch (IOException ie) { System.err.printf("I/O exception", ie.getMessage()); } try (FileOutputStream fopst = new FileOutputStream("F:\\EduCBA\\April\\Eduou.txt")) { Base64.Decoder dec = Base64.getDecoder(); InputStream inpt2 = dec.wrap(new FileInputStream("F:\\EduCBA\\April\\Eduout.txt")); int b1; while ((b1 = inpt2.read()) != -1) fopst.write(b1); inpt2.close(); } catch (IOException ie) { System.err.printf("I/O exception", ie.getMessage()); } } }
Output:
In this program, create three create text files, Edu, Eduout, Eduou, in a location for storing data, encoded data, and decoded data respectively. The below figure is the input data.
The encoded and decoded data will be written into the two other files on executing the code, as shown below.
以上是Java Base64 解碼的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于结构化数据处理开源库SPL的相关问题,下面就一起来看一下java下理想的结构化数据处理类库,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于PriorityQueue优先级队列的相关知识,Java集合框架中提供了PriorityQueue和PriorityBlockingQueue两种类型的优先级队列,PriorityQueue是线程不安全的,PriorityBlockingQueue是线程安全的,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于java锁的相关问题,包括了独占锁、悲观锁、乐观锁、共享锁等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于多线程的相关问题,包括了线程安装、线程加锁与线程不安全的原因、线程安全的标准类等等内容,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于枚举的相关问题,包括了枚举的基本操作、集合类对枚举的支持等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Java的相关知识,其中主要介绍了关于关键字中this和super的相关问题,以及他们的一些区别,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于平衡二叉树(AVL树)的相关知识,AVL树本质上是带了平衡功能的二叉查找树,下面一起来看一下,希望对大家有帮助。

封装是一种信息隐藏技术,是指一种将抽象性函式接口的实现细节部分包装、隐藏起来的方法;封装可以被认为是一个保护屏障,防止指定类的代码和数据被外部类定义的代码随机访问。封装可以通过关键字private,protected和public实现。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

Dreamweaver CS6
視覺化網頁開發工具

WebStorm Mac版
好用的JavaScript開發工具