【发布时间】:2015-05-12 22:53:20
【问题描述】:
我有一个使用 JSON 发布到 Spring 表单中的 base64 编码字符串。
data:image/png;base64,iVBORw0KGg......etc
我想将此图像添加为电子邮件的附件。附加文件工作正常,但它只是添加 base64 字符串作为附件。
我正在使用以下代码来创建附件部分。
private MimeBodyPart addAttachment(final String fileName, final String fileContent) throws MessagingException {
if (fileName == null || fileContent == null) {
return null;
}
LOG.debug("addAttachment()");
MimeBodyPart filePart = new MimeBodyPart();
String data = fileContent;
DataSource ds;
ds = new ByteArrayDataSource(data.getBytes(), "image/*");
// "image/*"
filePart.setDataHandler(new DataHandler(ds));
filePart.setFileName(fileName);
LOG.debug("addAttachment success !");
return filePart;
}
我也试过了
ds = new ByteArrayDataSource(data, "image/*");
如何使用 ByteArrayDataSource 将 base64 字符串转换为正确的图像文件?
【问题讨论】:
标签: java jakarta-mail