在Java中创建Base64字符串时出现OutOfMemory吗? [英] OutOfMemory when creating Base64 string in java?

查看:83
本文介绍了在Java中创建Base64字符串时出现OutOfMemory吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用ostermillerutils库创建base64字符串,但是如果图像很重,则会出现OutOfMemory错误.如果我尝试转换的图像是简单图像,则代码工作正常.

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 fine.

public String createBase64String(InputStream in) {
    //collect = new ByteArrayOutputStream();
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    byte[] buf = new byte[1024];
    try {
        for(int readNum; (readNum = in.read(buf)) != -1; ) {
            bos.write(buf, 0, readNum);
        }
    }
    catch (IOException ex) {
        Logger.getInstance().debug("XML createBase64String: IOException");
        return null;
    }
    finally {
        if (in != null) {
            try {
                in.close();

            }
            catch (IOException ex) {
                ;
            }
        }
    }
    byte[] ba = bos.toByteArray();
    String coded = Base64.encodeToString(ba);
    return coded;
}

我也尝试这样做,但是当我尝试对其进行解码时,base64是不正确的.

I also tried doing this but the base64 was incorrect when I tried to decode it.

public void createBase64String(InputStream in) throws IOException {
    //collect = new ByteArrayOutputStream();

    byte[] buf = new byte[1024];
    int readNum = 0;

    try {
        while((readNum = in.read(buf)) != -1)  
         {    
            smtp.addBase64(Base64.encodeBase64String(buf));
         }  
    }
    catch (IOException ex) {
        Logger.getInstance().debug("XML createBase64String: IOException");
    }
    finally {
        if (in != null) {
            in.close();
        }
    }

}

请为JDK 1.4和更高版本的Java建议解决方案.

Please suggest solutions for JDK 1.4 and also for later versions of Java.

推荐答案

我已经通过使用javabase64-1.3.1.jar库解决了我的问题.

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();

我首先将base64字符串存储到文本文件中.

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;
}

然后将每一行发送到smtp的套接字输出流.

then send each line to smtp's socket outputstream.

这篇关于在Java中创建Base64字符串时出现OutOfMemory吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆