[英]java mail Base64 encoded string to image attachment
I have a base64 encoded string which is posted using JSON into a Spring form. 我有一个base64编码的字符串,使用JSON发布到Spring表单中。
data:image/png;base64,iVBORw0KGg......etc
I want to add this image as an attachment to an email. 我想将此图像添加为电子邮件的附件。 Attaching the file works fine, but it is just adding the base64 string as the attachment.
附加文件工作正常,但它只是添加base64字符串作为附件。
I am using the following code to create the attachment part. 我使用以下代码来创建附件部分。
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;
}
I also tried 我也试过了
ds = new ByteArrayDataSource(data, "image/*");
How can I convert the base64 string into a proper image file using the ByteArrayDataSource ? 如何使用ByteArrayDataSource将base64字符串转换为正确的图像文件?
You'll hav to use a Base64-decoder first. 你将首先使用Base64解码器。 With Java 8 you could do:
使用Java 8,您可以:
byte[] imgBytes = Base64.getDecoder().decode(base64String);
With older java-versions you'll have to use some library like apache commons-codec or something - there's lots of those around. 对于较旧的java版本,你将不得不使用一些像apache commons-codec这样的库 - 其中有很多这样的库。
To avoid decoding and re-encoding you can use the javax.mail.internet.PreencodedMimeBodyPart to load your base64 string and attach the PreencodedMimeBodyPart to your message. 要避免解码和重新编码,可以使用javax.mail.internet.PreencodedMimeBodyPart加载base64字符串并将PreencodedMimeBodyPart附加到邮件中。
private MimeBodyPart addAttachment(final String fileName, final String fileContent) throws MessagingException {
if (fileName == null || fileContent == null) {
return null;
}
LOG.debug("addAttachment()");
MimeBodyPart filePart = new PreencodedMimeBodyPart("base64");
filePart.setContent(fileContent, "image/*");
LOG.debug("addAttachment success !");
return filePart;
}
Otherwise, you can use the javax.mail.internet.MimeUtility::decode to wrap the input stream used with your data source but this will decode and re-encode the given data. 否则,您可以使用javax.mail.internet.MimeUtility :: decode来包装与数据源一起使用的输入流,但这将解码并重新编码给定数据。
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; //Assuming fileContent was encoded as UTF-8.
InputStream in = new ByteArrayInputStream(data.getBytes("UTF-8"));
try {
in = MimeUtility.decode(in, "base64");
try {
ds = new ByteArrayDataSource(in , "image/*");
} finally {
in.close();
}
} catch (IOException ioe) {
throw new MessagingException(fileName, ioe);
}
// "image/*"
filePart.setDataHandler(new DataHandler(ds));
filePart.setFileName(fileName);
LOG.debug("addAttachment success !");
return filePart;
}
// v = data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA........................
String body = v.replace("data:image/png;base64","");
MimeBodyPart filePart = new PreencodedMimeBodyPart("base64");
filePart.setFileName("screenshot.png");
//This is Needed if you want to show as an html element in the page
filePart.setHeader("Content-ID", "<screenshot>");
filePart.setText(body);
message.getMimeMultipart().addBodyPart(filePart);
i encounter the same issue and I was able to fix it using this code: 我遇到了同样的问题,我能够使用此代码修复它:
//you have to parse the data first to remove "data:image/png;base64" before you can use the decodeBase64(data). //在使用decodeBase64(数据)之前,必须首先解析数据以删除“data:image / png; base64”。
byte[] imgBytes = Base64.decodeBase64(data);
ByteArrayDataSource dSource = new ByteArrayDataSource(imgBytes, "image/*");
With the answer of @jmehrens I was able to attach a file, but they couldn't be opened. 有了@jmehrens的答案,我能够附加一个文件,但是无法打开它们。 Turned out you need to set the datatype seperately and remove it from the given fileContent String:
原来你需要单独设置数据类型并从给定的fileContent字符串中删除它:
{
String dataType = StringUtils.substringBetween(file, "data:", ";base64,"); // extract data type (dataType = "image/png")
base64EncodedFileContent = fileContent.replaceFirst("data:.*;base64,", ""); // remove prefix from fileContent String (base64EncodedFileContent = "iVBORw0KGg......etc"
MimeBodyPart filePart = new PreencodedMimeBodyPart("base64");
filePart.setContent(base64EncodedFileContent, dataType);
filePart.setFileName(fileName);
return filePart;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.