How to find the path to a .txt file in glassfish v3.0
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)
Put emailActivationTemplate.txt in WEB-INF/classes, and get it with
BufferedReader br = new BufferedReader(new InputStreamReader(Thread.currentThread().getContextClassLoader().getResource("emailActivationTemplate.txt"));
(String) System.getProperties().get("com.sun.aas.instanceRoot")
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.
精彩评论