How to find the path to a .txt file in glassfish v3.0

十年热恋 提交于 2019-12-25 03:57:16

问题


In my aplication i want to send a html template to the users email. Everithing works correctly when i programatically create the html, but what i want to do now, is read the html text from within a file in my application and send it. I get a FileNotFoundException, and i dont know how to find that .txt file. See the code:

public void sendAccountActivationLinkToBuyer(String destinationEmail,
        String name) {

    // Destination of the email
    String to = destinationEmail;
    String from = "myEmail@gmail.com";

    try {
        Message message = new MimeMessage(mailSession);
        // From: is our service
        message.setFrom(new InternetAddress(from));
        // To: destination given
        message.setRecipients(Message.RecipientType.TO,
                InternetAddress.parse(to));
        message.setSubject("Registration succeded");
        // Instead of simple text, a .html template should be added here!
        message.setText(generateActivationLinkTemplate());

        Date timeStamp = new Date();
        message.setSentDate(timeStamp);

        Transport.send(message);

    } catch (MessagingException e) {
        throw new RuntimeException(e);
    }

}

private String generateActivationLinkTemplate() {
    String htmlText = "";

    try {
        File f = new File("");
        BufferedReader br = new BufferedReader(new InputStreamReader(f.getClass().getResourceAsStream("./web/emailActivationTemplate.txt")));
        String content = "";
        String line = null;

        while ((line = br.readLine()) != null) {
            content += line;
        }

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return htmlText;
}

The second method is giving me problems, i cant find that .txt file. What should i do? I created the folder web inside the WebContent folder,the web folder is now located right next to META-INF and WEB-INF(I think that is an appropiate place to hold my images, templates,css...) Inside the folder i manually pasted the emailActivationTemplate.txt Now i need to read from it. Any ideas?

This is the console output:

SEVERE: java.io.FileNotFoundException: .\web\emailActivationTemplate.txt (The system cannot find the path specified)


回答1:


Put emailActivationTemplate.txt in WEB-INF/classes, and get it with

BufferedReader br = new BufferedReader(new InputStreamReader(Thread.currentThread().getContextClassLoader().getResource("emailActivationTemplate.txt"));



回答2:


(String) System.getProperties().get("com.sun.aas.instanceRoot")



回答3:


Your emailActivationTemplate.txt should be present inside the classes folder of WEB-INF. If you manage to place it there, you should be able to read it using:

BufferedReader br = new BufferedReader(new InputStreamReader(this.getClass().getClassLoader().getResourceAsStream("/emailActivationTemplate.txt")));

Try without the leading '/' if it doesn't work.



来源:https://stackoverflow.com/questions/5246472/how-to-find-the-path-to-a-txt-file-in-glassfish-v3-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!