OutOfMemory when creating Base64 string in java?

后端 未结 4 1694
清酒与你
清酒与你 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:05

    I've fixed my problem by using javabase64-1.3.1.jar library.

    OutputStream fos2 = FileUtil.getOutputStream(base64FileName, FileUtil.HDD);
    InputStream  in2 = FileUtil.getInputStream(fileName, FileUtil.HDD);
    Base64.encode(in2, fos2);
    in2.close();
    fos2.close();
    

    I stored the base64 string to a text file first.

    public void createBase64String(InputStream in) throws IOException {
        baos = new ByteArrayOutputStream();
        byte[] buf = new byte[BUFFER_SIZE];
        int readNum = 0;
        smtp.addBase64("\t\t");
    
        try {
            while ((readNum = in.read(buf)) >= 0) {
                baos.write(buf, 0, readNum);
                smtp.addBase64(baos.toString());
                baos.reset();
            }
        }
        catch (IOException ex) {
            LogUtil.error("Sending of Base64 String to SMTP: IOException: " + ex);
        }
        finally {
            if (in != null) {
                in.close();
                baos.close();
            }
        }
    
        baos = null;
        buf = null;
    }
    

    then send each line to smtp's socket outputstream.

提交回复
热议问题