I have the following byte[]that comes from the database.
0x255044462D312E330A25AAABAC
Note: the above byte array is an example of a complete file not due to length.
UPDATE:
But I get the format [B@7ffd10fa
- Before you see the code, read here:
When I send bytes, which returns a method getPdfByteStream(), it sends the attachment by email as the source file. But when I get from the database and send it, the damaged file is sent.
UPDATE:
Entity.class
@Lob
@Column(name = "ATTACHED_FILE")
private byte[] attachedFile;
Email Code
try {
MimeBodyPart textBodyPart = new MimeBodyPart();
textBodyPart.setText(content);
**UPDATE:**
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();
}
getPDFByteStream () method
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;
}
Can anyone guide me
- , . .
, - .
.