簡體   English   中英

使用 Base64 編碼/解碼 java 中的文件

[英]encode/decode file in java with Base64

我在 Java 中編寫了一個編碼/解碼文件,如下所示

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Base64;

public class Test {
    
    public static void encodeFile(String inputfile, String outputfile) throws IOException {
        byte[] input_file = Files.readAllBytes(Paths.get(inputfile));
        byte[] encodedBytes = Base64.getEncoder().encode(input_file);
        String encodedString =  new String(encodedBytes);
        
        File ff =  new File(outputfile);
        FileOutputStream fileOut = new FileOutputStream(ff);
        OutputStreamWriter outStream = new OutputStreamWriter(fileOut);
        outStream.write(encodedString);
        outStream.flush();
    }
    
    public static void decodeFile(String encodedfilecontent, String decodedfile) throws IOException {   
        byte[] decoded = Base64.getDecoder().decode(encodedfilecontent);
        String decodedString =  new String(decoded);
        
        File ff =  new File(decodedfile);
        FileOutputStream fileOut = new FileOutputStream(ff);
        OutputStreamWriter outStream = new OutputStreamWriter(fileOut);
        outStream.write(decodedString);
        outStream.flush();
    }

    public static void main(String[] args) throws IOException {
        
        String inputfile = "C:\\Users\\John\\Desktop\\Files.zip";
        
        String outputfile = "C:\\Users\\John\\Desktop\\encoded.txt";
        
        encodeFile(inputfile, outputfile);
        
        String encodedfilecontent = new String(Files.readAllBytes(Paths.get(outputfile)));
        
        String decodedfile = "C:\\Users\\John\\Desktop\\DecodedFiles.zip";
        
        decodeFile(encodedfilecontent, decodedfile);

    }

}

上面的代碼有2個方法:
1-將文件編碼為 Base64 並將其寫入文本文件
2-解碼文本文件並將其寫回新文件
所有輸入/輸出文件都在桌面
我已經對此進行了測試,並且僅當 inputfile 是簡單的文本文件時,此編碼和解碼方法才有效。 如果輸入文件是圖像或像本例這樣的 zip 文件,則解碼文件將被破壞。 你能解釋一下為什么它會這樣壞嗎?

無論如何,是否可以將任何類型的文件普遍編碼為 Base64 並將其解碼回來? 如果是,您可以調整上面的代碼來做到這一點嗎?

在您的 decodeFile 方法中,您不應將 byte[] 轉換為字符串。 這將使用默認的平台字符編碼,並且某些字節在該編碼中可能沒有意義。 相反,您應該直接在 output 文件中寫入字節數組。

您沒有關閉文件。 當您對二進制數據使用文本(字符串/讀取器/寫入器)時,還會出現上述問題:數據損壞、速度較慢、雙 memory、在未指定編碼時取決於平台。

最佳解決方案是不取 memory 中的字節,另外制作一個以 64 為基數的 8/5 大字節數組。

使用 try-with-resources 自動關閉文件,即使出現異常(如非法 Base 64 字符)。

public static void encodeFile(String inputFile, String outputFile)
        throws IOException {
    Path inPath = Paths.get(inputFile);
    Path outPath = Paths.get(outputFile);
    try (OutputStream out = Base64.getEncoder().wrap(Files.newOutputStream(outPath))) {
        Files.copy(inPath, out);
    }
}

public static void decodeFile(String encodedfilecontent, String decodedfile)
        throws IOException {
    Path inPath = Paths.get(encodedfilecontent);
    Path outPath = Paths.get(decodedfile);
    try (InputStream in = Base64.getDecoder().wrap(Files.newInputStream(inPath))) {
        Files.copy(in, outPath);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM