OutOfMemory when creating Base64 string in java?

后端 未结 4 1747
清酒与你
清酒与你 2021-01-24 07:48

I used ostermillerutils library to create base64 string but I get OutOfMemory error if the image is heavy. If the image I try to convert is a simple image, the code is working f

4条回答
  •  北荒
    北荒 (楼主)
    2021-01-24 08:15

    From Java 8 onwards, there is a simple way to implement base64 encoding in an output stream with one line of code and no external dependencies:

       import java.util.Base64;
    
       OutputStream os = ...
       OutputStream base64 = Base64.getEncoder().wrap(os);
    

    Base64 also provides other flavors of base64 encoder; see javadocs:

    • Base64
    • Base64.Encoder.wrap

提交回复
热议问题