gpt4 book ai didi

java - 从 MimeMultipart 解码 Base64 内容

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:37:27 24 4
gpt4 key购买 nike

我在 MimeMessage 的一部分中有一个 Base64 文件。我是这样写的

DataSource source = new FileDataSource(new File("base64Test.bin"));
bodyPart.setDataHandler(new DataHandler(source));

然后,我想使用 BASE64DecoderStream.decode 方法对其进行解码

String base64 = (String)bodyPart.getContent();
byte [] base64Decoder = BASE64DecoderStream.decode(base64.getBytes());

问题在于,当我使用此方法时,我总是遇到 ArrayOutOfboundexception

有什么建议可以解决这个问题吗?

最佳答案

据我所知,Base64DecoderStream 采用 InputStream,而不是字节数组。至少,您需要像这样更改解码:

ByteArrayInputStream contentStream = new ByteArrayInputStream(base64Content.getBytes());
BASE64DecoderStream decodeStream = new BASE64DecoderStream(contentStream);
int decodedByte
while ((decodedByte = read()) != -1) {
// handled decoded bytes
}

也许自动装箱正试图帮助您解决问题,而一些“中间”数组创建正在破坏工作。或者,也许您的消息足够大,以至于无法创建足够大的数组。或者您有两个线程调用静态产品,这会弄乱内部共享缓冲区。

关于java - 从 MimeMultipart 解码 Base64 内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6468839/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com