Cannot retrieve images from WEB-INF using class Loader.getResourceAsStream ()

In the afternoon, I tried to make my application sent as html + images via javamail, I only managed to send html, but I had problems with the image. I decided to create a multi-page message and everything went fine, but then I use the class loader to extract the .png file from WEB-INF / resources / images, I get NullPointerExcetion, I don’t know why this is?

This is what my EJB (3.0) looks like. I really appreciate the hand on this, I don’t have much experience with the ClassLoader class (I don’t know much about it).

@Stateless(name = "ejbs/EmailServiceEJB")
public class EmailServiceEJB implements IEmailServiceEJB {

@Resource(name = "mail/myMailSession")
private Session mailSession;

public void sendAccountActivationLinkToBuyer(String destinationEmail,
        String name) {

    // Destination of the email
    String to = destinationEmail;
    String from = "dontreply2thismessage@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("Uspijesna registracija");
        // How to found at http://www.rgagnon.com/javadetails/java-0321.html
        message.setContent(generateActivationLinkTemplate(), "text/html");

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

        // Prepare a multipart HTML
        Multipart multipart = new MimeMultipart();
        // Prepare the HTML
        BodyPart htmlPart = new MimeBodyPart();
        htmlPart.setContent(generateActivationLinkTemplate(), "text/html");
        multipart.addBodyPart(htmlPart);
        // PREPARE THE IMAGE
        BodyPart imgPart = new MimeBodyPart();

        String fileName = "/WEB-INF/resources/images/logoemailtemplate.png";

        ClassLoader classLoader = Thread.currentThread()
                .getContextClassLoader();
        if (classLoader == null) {
            classLoader = this.getClass().getClassLoader();
            if (classLoader == null) {
                System.out.println("IT IS NULL AGAIN!!!!");
            }
        }



        DataSource ds = new URLDataSource(classLoader.getResource(fileName));

        imgPart.setDataHandler(new DataHandler(ds));
        imgPart.setHeader("Content-ID", "the-img-1");
        multipart.addBodyPart(imgPart);
        // Set the message content!
        message.setContent(multipart);

        Transport.send(message);

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

}

I would like to mention that I use JEE6 with glassfishV3, I do not know if my approach is compatible with this application server.


String fileName = "logoemailtemplate.png";

, .

.:) ?

+3
3

, ClassLoader#getResourceAsStream() ServletContext#getResourceAsStream(). classpath, webcontent (, /WEB-INF).

. IDE, Java. /WEB-INF/classes , .

, com.example.resources.images, logoemailtemplate.png, fileName.

String fileName = "/com/example/resources/images/logoemailtemplate.png";

/WEB-INF/resources . IDE, Eclipse, , . fileName.

String fileName = "/images/logoemailtemplate.png";

, , .

+6

, classLoader WEB-INF/ WEB-INF/lib, WEB-INF/. .

+2

ServletContext.getResourceAsStream() . ClassLoader.getResourceAsStream .

0

All Articles