java 将 Base64 编码的字符串邮寄到图片附件
java mail Base64 encoded string to image attachment
我有一个 base64 编码的字符串,它使用 JSON 以 Spring 形式发布。
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 字符串转换为正确的图像文件?
您必须先使用 Base64 解码器。使用 Java 8 你可以这样做:
byte[] imgBytes = Base64.getDecoder().decode(base64String);
对于较旧的 java 版本,您将不得不使用一些库,例如 apache commons-codec 或其他东西 - 周围有很多这样的库。
为避免解码和重新编码,您可以使用 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;
}
否则,您可以使用 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;
}
我遇到了同样的问题,我可以使用以下代码修复它:
//必须先解析数据去掉"data:image/png;base64"才能使用decodeBase64(data).
byte[] imgBytes = Base64.decodeBase64(data);
ByteArrayDataSource dSource = new ByteArrayDataSource(imgBytes, "image/*");
// 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);
根据@jmehrens 的回答,我可以附加文件,但无法打开。结果你需要单独设置数据类型并将其从给定的 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;
}
我有一个 base64 编码的字符串,它使用 JSON 以 Spring 形式发布。
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 字符串转换为正确的图像文件?
您必须先使用 Base64 解码器。使用 Java 8 你可以这样做:
byte[] imgBytes = Base64.getDecoder().decode(base64String);
对于较旧的 java 版本,您将不得不使用一些库,例如 apache commons-codec 或其他东西 - 周围有很多这样的库。
为避免解码和重新编码,您可以使用 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;
}
否则,您可以使用 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;
}
我遇到了同样的问题,我可以使用以下代码修复它:
//必须先解析数据去掉"data:image/png;base64"才能使用decodeBase64(data).
byte[] imgBytes = Base64.decodeBase64(data);
ByteArrayDataSource dSource = new ByteArrayDataSource(imgBytes, "image/*");
// 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);
根据@jmehrens 的回答,我可以附加文件,但无法打开。结果你需要单独设置数据类型并将其从给定的 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;
}