java java邮件Base64编码的字符串到图像附件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28981724/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java mail Base64 encoded string to image attachment
提问by Coen Damen
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 字符串转换为正确的图像文件?
回答by jmehrens
To avoid decoding and re-encoding you can use the javax.mail.internet.PreencodedMimeBodyPartto 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::decodeto 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;
}
回答by piet.t
You'll hav to use a Base64-decoder first. With Java 8 you could do:
您必须首先使用 Base64 解码器。使用 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 或其他东西-周围有很多库。
回答by Balakrishna Vundavalli
// 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);
回答by coriefe
With the answer of @jmehrens I was able to attach a file, but they couldn't be opened. Turned out you need to set the datatype seperately and remove it from the given fileContent String:
通过@jmehrens 的回答,我能够附加一个文件,但无法打开它们。原来您需要单独设置数据类型并将其从给定的文件内容字符串中删除:
{
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;
}
回答by arn-arn
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).
//您必须先解析数据以删除“data:image/png;base64”,然后才能使用decodeBase64(data)。
byte[] imgBytes = Base64.decodeBase64(data);
ByteArrayDataSource dSource = new ByteArrayDataSource(imgBytes, "image/*");