从 MimeMultipart 解码 Base64 内容

发布于 11-17 01:08 字数 478 浏览 5 评论 0原文

我的 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 。

为了解决这个问题有什么建议吗?

I have a Base64 file into one part of my MimeMessage.
I put it like this

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

Then, I want to decode it using the method BASE64DecoderStream.decode

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

The problem with that is that I have always an ArrayOutOfboundexception when I use this method.

Any advice in order to solve this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蓦然回首2024-11-24 01:08:48

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

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

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

As far as I know, a Base64DecoderStream takes an InputStream, not a byte array. At a minimum, you need to change the decoding like so:

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

Perhaps autoboxing is trying to help you out, and some "intermediate" array creation is fouling the works. Or, maybe your message is just large enough that an array can't be created big enough. Or maybe you have two threads calling the static offering which is messing up internal shared buffers.

坚持沉默2024-11-24 01:08:48

我这样读取 BASE64DecoderStream:

DataHandler handler = bodyPart.getDataHandler();
System.out.println("attachment name : " + handler.getName());
                    
FileOutputStream fos = new FileOutputStream(new File(handler.getName()));
handler.writeTo(fos);

此 OutPutStream 具有已写入文件的数据。

I read the BASE64DecoderStream like this:

DataHandler handler = bodyPart.getDataHandler();
System.out.println("attachment name : " + handler.getName());
                    
FileOutputStream fos = new FileOutputStream(new File(handler.getName()));
handler.writeTo(fos);

this OutPutStream is having data which has been written to file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文