通过java mail发送附件包含byte[]的邮件

vbopmzt1  于 2023-01-01  发布在  Java
关注(0)|答案(2)|浏览(220)

我有以下byte[]来自数据库。

0x255044462D312E330A25AAABAC
    • 注意:上述字节数组是示例完整文件,由于长度原因,此处未显示。**
    • 更新日期:**

但我喜欢[B@7ffd10fa

  • 在您看到代码之前,请阅读此处:

当我发送返回getPdfByteStream()方法的bytes时,它会像原始文件一样在电子邮件中发送附件。但当我从数据库中获取并发送时,它会发送损坏的文件。

    • 更新日期:**
  • 实体类 *
@Lob
@Column(name = "ATTACHED_FILE")
private byte[] attachedFile;

//getter()/setter();
    • 发送电子邮件的代码**
try {
        MimeBodyPart textBodyPart = new MimeBodyPart();
        textBodyPart.setText(content);

       //byte[] bytes = getPDFByteStream(); //Returns byte[] reading local drive file

         **UPDATE:**

        //bytes[] bytes=entity.getAttachedFile(); // It gets value from entity.

        /**
        ** It is getting like "[B@7ffd10fa" format but m storing on database like "0x255044462D312E330A25" format
        **/

        String string="0x255044462D312E330A25";
        byte[] bytes =string.getBytes(Charset.forName("UTF-8"));
        System.out.println("bytes " + bytes.toString());

        DataSource dataSource = new ByteArrayDataSource(bytes, "application/pdf");
        MimeBodyPart pdfBodyPart = new MimeBodyPart();
        pdfBodyPart.setDataHandler(new DataHandler(dataSource));
        pdfBodyPart.setFileName("bankAdminReport.pdf");

        MimeMultipart mimeMultipart = new MimeMultipart();
        mimeMultipart.addBodyPart(textBodyPart);
        mimeMultipart.addBodyPart(pdfBodyPart);

        InternetAddress iaSender = new InternetAddress(sender);
        InternetAddress iaRecipient = new InternetAddress(recipient);

        MimeMessage mimeMessage = new MimeMessage(session);
        mimeMessage.setSender(iaSender);
        mimeMessage.setSubject(subject);
        mimeMessage.setRecipient(Message.RecipientType.TO, iaRecipient);
        mimeMessage.setContent(mimeMultipart);

        Transport.send(mimeMessage);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    • 获取PDF字节流()方法**
public static byte[] getPDFByteStream() throws IOException {
    File file = new File("C:\\pdf\\bankAdminReport.pdf");

    byte[] b = new byte[(int) file.length()];
    try {
        FileInputStream fileInputStream = new FileInputStream(file);
        fileInputStream.read(b);
    } catch (FileNotFoundException e) {
        System.out.println("File Not Found.");
        e.printStackTrace();
    } catch (IOException e1) {
        System.out.println("Error Reading The File.");
        e1.printStackTrace();
    }
    return b;
}

有人能指引我吗?
主要问题是当我发送文件读取从本地驱动器发送完美。但如果我发送从数据库或任何本地变量文件损坏获取。
如有任何疑问,请在下方留言。谢谢。

weylhg0b

weylhg0b1#

这个

byte[] bytes="0x255044462D312E330A25AAABAC".getBytes();

也会对前导"0x"进行编码。此外,看起来您正在尝试将十六进制值转换为字节数组,而此方法会将字符值转换为字节。
我相信你要找的是

byte[] bytes = java.xml.bind.DatatypeConverter.parseHexBinary("255044462D312E330A25AAABAC");

试试看。

mhd8tkvw

mhd8tkvw2#

Imo,这是一个字符串转换的问题...下面的例子说明了一种方法-也许不是最聪明的:-D

public class TestString2Binary {
    public static void main(String[] args) {
        String testText="This is \n a sample";
        System.out.println("String.toString(): ");
        System.out.println(testText);
        byte[] b=testText.getBytes();
        System.out.println("byte[]-toString(): ");
        System.out.println(b);

        System.out.println("byte[] values - toString(): ");
        for (byte x:b) {
            if (x<100)
                System.out.print("0"+x);
            else
                System.out.print(x);
        }

        String s="084104105115032105115032010032097032115097109112108101";
        System.out.println("imo the back converting to String goes wrong:");
        System.out.println(s.getBytes());
        System.out.println(new String(s.getBytes()));
        System.out.println(s.getBytes(Charset.forName("UTF-8")));
        System.out.println(new String(s.getBytes(Charset.forName("UTF-8"))));

        int recoveredBLength=s.length()/3;
        byte[] recoveredB=new byte[recoveredBLength];
        for (int i=0;i<recoveredBLength;i++) {
            String part=s.substring(i*3,(i*3)+3);
            recoveredB[i]=Byte.parseByte(part);
        }

        System.out.println("the original string: ");
        System.out.println(new String(recoveredB));

    }
}

相关问题