Out of memory when encoding file to base64

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

Using Base64 from Apache commons

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


        
8条回答
  •  萌比男神i
    2020-11-30 04:15

    1. You are not reading the whole file, just the first few kb. The read method returns how many bytes were actually read. You should call read in a loop until it returns -1 to be sure that you have read everything.

    2. The file is too big for both it and its base64 encoding to fit in memory. Either

      • process the file in smaller pieces or
      • increase the memory available to the JVM with the -Xmx switch, e.g.

        java -Xmx1024M YourProgram
        

提交回复
热议问题