我需要使用这个类ByteArrayDataSource来发送一个带有附件的电子邮件(一个用iText创建的pdf ),但是我们的环境是在Java1.4上运行的,但是这个类是在Javamail中需要更高的版本。
我必须使用这个类,如下所示:
//now write the PDF content to the output stream
outputStream = new ByteArrayOutputStream();
pdfCreator.createPdf(data,outputStream);
byte[] bytes = outputStream.toByteArray();
//construct the pdf body part
DataSource dataSource = **new ByteArrayDataSource**(bytes, "application/pdf");
MimeBodyPart pdfBodyPart = new MimeBodyPart();
pdfBodyPart.setDataHandler(new DataHandler(dataSource));
pdfBodyPart.setFileName("listadosCitaciones.pdf");
multipart.addBodyPart(messageBodyPart);有什么建议吗?
发布于 2012-07-20 17:48:17
你应该能够自己从头开始实现一个等价的类。查看DataSource接口中的方法的javadoc,您需要如何实现它们应该是显而易见的。
(我会为你做的,但我已经为这周写了足够多无聊的代码:-)
发布于 2012-07-20 18:29:28
Stephen是对的,您只需要实现一个自定义数据源,如下所示:
public class ByteArrayDataSource implements DataSource {
public ByteArrayDataSource(byte[] b, String ct) {
bytes = b;
contentType = ct;
}
public String getContentType() {
return contentType;
}
public InputStream getInputStream() throws IOException {
return new ByteArrayInputStream(bytes);
}
public String getName() {
return null;
}
public OutputStream getOutputStream() {
throw new UnsupportedOperationException();
}
private byte[] bytes;
private String contentType;
}然后你可以像使用jdk1.5 ByteArrayDataSource一样使用它。
https://stackoverflow.com/questions/11576578
复制相似问题