浏览 1975 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2011-10-08
1、版本要一致,偶这用的是jasperreports-4.1.2,iReport-4.1.1 2、编译好***.jasper 3、/html/image.jsp?image= 替换,是为了显示报表图片,之前用此jsp作为读取报表图片用。 public void process() { Connection conn = null; try { //报表内嵌了SQL,只要传入参数,数据库连接即可。 conn = dataSource.getConnection(); Map parameters = new HashMap(); parameters.put("startTime", "20110101"); parameters.put("endTime", "20111001"); String jasperFile = "d://*****/" + "*****.jasper"; File reportFile = new File(jasperFile); if (!reportFile.exists()) throw new JRRuntimeException("File jasper not found. The report design must be compiled first."); JasperPrint jasperPrint = JasperFillManager.fillReport( jasperFile, parameters, conn ); JRHtmlExporter exporter = new JRHtmlExporter(); exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); ByteArrayOutputStream htmlOutputData = new ByteArrayOutputStream(); exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, htmlOutputData); exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, imageUrl); exporter.setParameter(JRHtmlExporterParameter.IMAGES_DIR_NAME, imagePath); exporter.setParameter(JRHtmlExporterParameter.IS_OUTPUT_IMAGES_TO_DIR, true); exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN, false); exporter.setParameter(JRExporterParameter.CHARACTER_ENCODING, "UTF-8"); Map imageNameMap = new HashMap(); exporter.setParameter(JRHtmlExporterParameter.IMAGES_MAP, imageNameMap); exporter.exportReport(); //attach the html data from htmlOutputData byte[] imageData = null; for (Iterator it = imageNameMap.entrySet().iterator(); it.hasNext(); ) { Map.Entry entry = (Map.Entry) it.next(); String imageName = (String) entry.getKey(); imageData = (byte[]) entry.getValue(); //attach imageData using imageName as Content-ID } sendEmail(htmlOutputData.toByteArray(), imageData, imageNameMap); } catch (Exception e) { e.printStackTrace(); } finally { try { if (conn != null) conn.close(); } catch (Exception e) { } } } public void sendEmail(final byte[] attachmentData, final byte[] imageData, final Map<String, byte[]> imageNameMap) throws MessagingException { String host = "smtp.126.com"; final String account = "****@126.com"; final String toAddress = "****@163.com"; String username = ""****@126.com"; String password = "******"; JavaMailSenderImpl sender = new JavaMailSenderImpl(); sender.setHost(host); sender.setUsername(username); sender.setPassword(password); MimeMessagePreparator preparator = new MimeMessagePreparator() { public void prepare(MimeMessage mimeMessage) throws Exception { Multipart multipart = new MimeMultipart(); MimeBodyPart htmlAttachment = new MimeBodyPart(); String html = new String(attachmentData, "utf-8"); for (String imageName : imageNameMap.keySet()) { html = html.replace("/html/image.jsp?image=" + imageName, "cid:" + imageName); } htmlAttachment.setContent(html, "text/html;charset=\"utf-8\""); // multipart.addBodyPart(htmlAttachment); //当你有多个报表图片时 for (String imageName : imageNameMap.keySet()) { MimeBodyPart imagebody = new MimeBodyPart(); imagebody.setContent(imageData, "application/octet-stream"); imagebody.setHeader("Content-ID", "<" + imageName + ">"); multipart.addBodyPart(imagebody); } mimeMessage.setContent(multipart); mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(toAddress)); mimeMessage.setFrom(new InternetAddress(account)); mimeMessage.setSubject("test send jasperreports mail.."); } }; try { sender.send(preparator); } catch (MailException ex) { ex.printStackTrace(); } } 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |