Java-从db和电子邮件中的字节数组中获取PDF作为附件

时间:2017-07-03 15:03:42

标签: java pdf javamail

美好的一天

我正在尝试从数据库的字节数组字段中获取pdf内容,然后通过电子邮件将其作为附件发送。目前,文件本身已生成,但附件为空。

当我执行part.getContent时,内容的实例是String类型,而不是ByteArrayInputStream类型。请协助。

static MimeBodyPart readBodyPart(java.io.ObjectInputStream in) throws IOException {
    MimeBodyPart part = null;

    final int sz = in.readInt();

    if (sz > 0) {
        byte[] buf = new byte[sz];
        in.readFully(buf);

        ByteArrayInputStream bis = new ByteArrayInputStream(buf);

        try {
            part = new MimeBodyPart(bis);

            final Object content = part.getContent();

            if (content instanceof ByteArrayInputStream) {
                final DataHandler dh = new DataHandler(new ByteArrayDataSource((ByteArrayInputStream)content, part.getContentType()));
                part.setDataHandler(dh);
            }
        }
        catch (javax.mail.MessagingException e) {
            throw new IOException(e);
        }
        finally {
            bis.close();
        }
    }

    return part;
}

0 个答案:

没有答案