2011-03-09 52 views
0

在我的應用程序中,我想將html模板發送給用戶電子郵件。當我以編程方式創建html時,Everithing能夠正常工作,但是現在我想要做的是從我的應用程序的文件中讀取html文本併發送它。我得到一個FileNotFoundException,並且我不知道如何找到.txt文件。看代碼:如何在glassfish v3.0中找到.txt文件的路徑

public void sendAccountActivationLinkToBuyer(String destinationEmail, 
     String name) { 

    // Destination of the email 
    String to = destinationEmail; 
    String from = "[email protected]"; 

    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; 
} 

第二種方法給我的問題,我無法找到.txt文件。我該怎麼辦? 我在WebContent文件夾中創建了文件夾Web,Web文件夾現在位於META-INF和WEB-INF旁邊(我認爲這是適當的地方來放置我的圖像,模板,CSS ......)我手動粘貼emailActivationTemplate.txt現在我需要讀取它。有任何想法嗎?

這是控制檯輸出:

嚴重:java.io.FileNotFoundException:\網絡\ emailActivationTemplate.txt(系統找不到指定的路徑)

+0

到底在哪你放置在文件'emailActivationTemplate.txt'怎麼做呢?你可以給我從AppServer根開始的文件的完整路徑嗎? – adarshr 2011-03-09 13:48:56

+0

目前位於C:\ jee6workspace \ BBS \ WebContent \ web \ emailActivationTemplate.txt 我認爲.css文件,圖片...應該放在該文件夾中。那是對的嗎? – sfrj 2011-03-09 13:53:55

+0

是啊,看到我發佈評論後:) – adarshr 2011-03-09 13:54:42

回答

3

把emailActivationTemplate.txt在WEB-INF/classes中,並與

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

emailActivationTemplate.txt應目前classes文件夾中的WEB-INF。如果你能有地方,你應該使用能夠閱讀:

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

嘗試沒有領先「/」,如果它不能正常工作。

+0

不工作這是輸出:引起:java.lang.NullPointerException \t在java.io.Reader。 (Reader.java:61) \t at java.io.InputStreamReader。 (InputStreamReader.java:55) \t在ejbs.EmailServiceEJB.generateActivationLinkTemplate(EmailServiceEJB.java:60) \t在ejbs.EmailServiceEJB.sendAccountActivationLinkToBuyer(EmailServiceEJB.java:43) – sfrj 2011-03-09 14:12:25

3
(String) System.getProperties().get("com.sun.aas.instanceRoot") 
相關問題