Out of memory when encoding file to base64

前端 未结 8 596
自闭症患者
自闭症患者 2020-11-30 03:46

Using Base64 from Apache commons

public byte[] encode(File file) throws FileNotFoundException, IOException {
        byte[] encoded;
        try (FileInputSt         


        
8条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-30 03:57

    Java 8 added Base64 methods, so Apache Commons is no longer needed to encode large files.

    public static void encodeFileToBase64(String inputFile, String outputFile) {
        try (OutputStream out = Base64.getEncoder().wrap(new FileOutputStream(outputFile))) {
            Files.copy(Paths.get(inputFile), out);
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }
    }
    

提交回复
热议问题