Java Encode file to Base64 string To match with other encoded string

We Are Going To Discuss About Java Encode file to Base64 string To match with other encoded string. So lets Start this Java Article.

Java Encode file to Base64 string To match with other encoded string

  1. Java Encode file to Base64 string To match with other encoded string

    You already using apache commons-codec so I recommend adding commons-io for reading the file. That way you can remove your loadFile() method and just have:

  2. Java Encode file to Base64 string To match with other encoded string

    You already using apache commons-codec so I recommend adding commons-io for reading the file. That way you can remove your loadFile() method and just have:

Solution 1

You already using apache commons-codec so I recommend adding commons-io for reading the file. That way you can remove your loadFile() method and just have:

private static String encodeFileToBase64Binary(String fileName) throws IOException {
    File file = new File(fileName);
    byte[] encoded = Base64.encodeBase64(FileUtils.readFileToByteArray(file));
    return new String(encoded, StandardCharsets.US_ASCII);
}

Original Author gfelisberto Of This Content

Solution 2

Here is a solution without any required dependencies (Apacha et al.) requiring only JDK 8+:

import java.util.Base64;
import java.nio.file.Files;

private static String encodeFileToBase64(File file) {
    try {
        byte[] fileContent = Files.readAllBytes(file.toPath());
        return Base64.getEncoder().encodeToString(fileContent);
    } catch (IOException e) {
        throw new IllegalStateException("could not read file " + file, e);
    }
}

Original Author Patrick Favre Of This Content

Solution 3

Since Java 8 you can use the class java.util.Base64 and the corresponding inner classes:

  • java.util.Base64.Encoder
  • java.util.Base64.Decoder

See JavaDoc: Base64-Doc

And a sample for the use: Example from Oracle

Original Author the hand of NOD Of This Content

Solution 4

This example worked great for me: https://grokonez.com/java/java-advanced/java-8-encode-decode-an-image-base64

public static String encoder(String filePath) {
        String base64File = "";
        File file = new File(filePath);
        try (FileInputStream imageInFile = new FileInputStream(file)) {
            // Reading a file from file system
            byte fileData[] = new byte[(int) file.length()];
            imageInFile.read(fileData);
            base64File = Base64.getEncoder().encodeToString(fileData);
        } catch (FileNotFoundException e) {
            System.out.println("File not found" + e);
        } catch (IOException ioe) {
            System.out.println("Exception while reading the file " + ioe);
        }
        return base64File;
    }

Original Author Big D. Of This Content

Conclusion

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

Siddharth

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment