【发布时间】:2017-08-05 10:45:14
【问题描述】:
我正在编写一个流程,以便在使用 Groovy 并已安装 apache.commons.mail.* 库的系统中向客户发送电子邮件(尽管如果绝对必要我可以安装其他库)。
问题是图像作为 base64 编码字符串传递给我。我考虑过将它们保存为图像,然后通过 URL (here are some examples of sending an email with attachments) 附加它们,但这似乎浪费了一个步骤来解码它们以重新编码。有没有办法直接附上图片?
我找到了 a few 其他 SO posts 和 similar questions,但他们似乎对我的情况给出了不满意的答案,或者依赖于我无法访问的其他 Java 版本/库。
编辑:
这就是我现在的状态(感谢 JerryP 对 this post 的评论):
def my_img = "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQAB...";
byte[] img_bytes = b64_data.src.drop(b64_data.src.indexOf(',')+1).decodeBase64(); // This extracts the encoded part
def type = b64_data.src.substring(b64_data.src.indexOf(':') + 1, b64_data.src.indexOf(';') ); // This extracts the data type from the long base64 string (i.e. 'data:image/jpeg;base64,/9j/4AAQSkZJRgABA...')
enc_attachments = ByteArrayDataSource( img_bytes, type );
所以,我现在有一个 ByteArrayDataSource 我的图像,但 the embed calls in apache commons 不要将 ByteArrayDataSource 作为参数。
【问题讨论】:
标签: groovy base64 mime apache-commons