首頁 > Java > java教程 > 主體

Java Base64 解碼

王林
發布: 2024-08-30 16:10:45
原創
860 人瀏覽過

在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解碼的不同方法。

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);
}
}
登入後複製

輸出:

Java Base64 解碼

執行程式碼時會列印數組的編碼和解碼結果。

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);
}}
登入後複製

輸出:

Java Base64 解碼

執行程式碼時會列印字串的編碼和解碼結果。

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));
}}
登入後複製

輸出:

Java Base64 解碼

執行程式碼時會列印位元組數組的編碼和解碼結果。

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();
}
}
登入後複製

輸出:

Java Base64 解碼

執行程式碼時列印的位元組緩衝區的編碼和解碼結果。

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.

Java Base64 解碼

The encoded and decoded data will be written into the two other files on executing the code, as shown below.

Java Base64 解碼

Java Base64 解碼

以上是Java Base64 解碼的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!