Out of memory when encoding file to base64

前端 未结 8 590
自闭症患者
自闭症患者 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 04:01

    Well, looks like your file is too large to keep the multiple copies necessary for an in-memory Base64 encoding in the available heap memory at the same time. Given that this is for a mobile device, it's probably not possible to increase the heap, so you have two options:

    • make the file smaller (much smaller)
    • Do it in a stram-based way so that you're reading from an InputStream one small part of the file at a time, encode it and write it to an OutputStream, without ever keeping the enitre file in memory.

提交回复
热议问题