本文介绍了如何在glassfish v3.0中找到.txt文件的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我想发送一个html模板给用户的电子邮件。当我以编程方式创建html时,Everithing能够正常工作,但是现在我想要做的是从我的应用程序的文件中读取html文本并发送它。我得到一个FileNotFoundException,并且我不知道如何找到.txt文件。查看代码:

  public void sendAccountActivationLinkToBuyer(String destinationEmail,
String name){

//电子邮件的目的地
字符串到=目的地电子邮件;
来自=myEmail@gmail.com的字符串;

try {
Message message = new MimeMessage(mailSession);
//发件人:是我们的服务
message.setFrom(新的InternetAddress(从));
// To:给出
的目标message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(to));
message.setSubject(注册成功);
//应该在这里添加一个.html模板,而不是简单的文本。
message.setText(generateActivationLinkTemplate());

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

Transport.send(message);

catch(MessagingException e){
抛出新的RuntimeException(e);
}

}

private String generateActivationLinkTemplate(){
String htmlText =;

尝试{
文件f =新文件();
BufferedReader br = new BufferedReader(new InputStreamReader(f.getClass()。getResourceAsStream(./ web / emailActivationTemplate.txt)));
String content =;
String line = null; ((line = br.readLine())!= null){
content + = line;

while
}

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

返回htmlText;
}

第二种方法给了我一些问题,我无法找到.txt文件。我该怎么办?
我在WebContent文件夹中创建了文件夹web,这个web文件夹现在位于META-INF和WEB-INF旁边(我认为这是一个适合我的图像,模板,CSS的适当位置)在文件夹内我手动粘贴emailActivationTemplate.txt现在我需要阅读它。任何想法?



这是控制台输出:


解决方案

在WEB-INF / classes中放置emailActivationTemplate.txt,并将它与

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


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:

解决方案

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

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

这篇关于如何在glassfish v3.0中找到.txt文件的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 12:58