簡體   English   中英

如何在項目文件夾中檢索pdf文件?

[英]how to retrieve pdf file inside project folder?

我試圖創建一個類,該類發送帶有模板pdf附件的電子郵件。 這是我的代碼。

import java.io.File;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

/**
 *
 * @author user
 */
public class AttachMailUtil {

    public static void sendMail(String to, String from,
            String subject, String body, boolean bodyIsHTML, File filename)
            throws MessagingException {

        // 1 - get a mail session
        Properties props = new Properties();
        // props.put("mail.transport.protocol", "smtp");

        props.put("mail.smtp.host", "mail.hexagon.com.ph");
        props.put("mail.smtp.port", 587);
        props.put("mail.smtp.auth", "true");

        Authenticator authenticator;
        authenticator = new Authenticator() {
            @Override
            public PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("info@hexagon.com.ph", "info@dm1nhgc001");//userid and password for "from" email address
            }
        };

        // props.setProperty("mail.user", "m.corbes@hexagon.com.ph");
        // props.setProperty("mail.password", "password");
        // props.put("mail.smtp.host", "localhost");
        //props.put("mail.smtp.port", 25);       
        Session session = Session.getDefaultInstance(props, authenticator);
        session.setDebug(true);

        // 2 - create a message
        Message message = new MimeMessage(session);
        message.setSubject(subject);
        if (bodyIsHTML) {
            message.setContent(body, "text/html");
        } else {
            message.setText(body);
        }

        // 3 - address the message
        Address fromAddress = new InternetAddress(from);
        Address toAddress = new InternetAddress(to);
        message.setFrom(fromAddress);
        message.setRecipient(Message.RecipientType.TO, toAddress);

        //sending email with 
        MimeBodyPart messageBodyPart2 = new MimeBodyPart();

        // File savePath = new File(getServletContext().getRealPath("/jasperreports/OvertimeIndividual.pdf"));
        filename = new File(getServletContext().getRealPath("/jasperreports/WorkOrder.pdf"));
        //   String filename = "SendAttachment.java";//change accordingly  
        DataSource source = new FileDataSource(filename);
        messageBodyPart2.setDataHandler(new DataHandler(source));
        messageBodyPart2.setFileName("OvertimeIndividual.pdf");

        //5) create Multipart object and add MimeBodyPart objects to this object      
        Multipart multipart = new MimeMultipart();

        multipart.addBodyPart(messageBodyPart2);

        //6) set the multiplart object to the message object  
        message.setContent(multipart);

        // 4 - send the message
        Transport.send(message);
    }

}

我在這部分有錯誤

File(getServletContext().getRealPath("/jasperreports/OvertimeIndividual.pdf"));

如何獲取項目文件夾中pdf文件的realpath? 任何幫助表示贊賞,謝謝。

您可以使用:

new File(ClassLoader.getSystemClassLoader().getResource("jasperreports/OvertimeIndividual.pdf").getPath());

要么

new File(ClassLoader.getSystemClassLoader().getResource("jasperreports/OvertimeIndividual.pdf").toURI());

您可以使用這種方式:

new File(AttachMailUtil.class.getResource("/jasperreports/OvertimeIndividual.pdf").getPath());

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM