Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

java mail Base64 encoded string to image attachment

I have a base64 encoded string which is posted using JSON into a Spring form.

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.

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 ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

    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.

    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;
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...