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:

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);
}

Leave a Comment