无法从WEB-INF文件夹中检索图像ussing classLoader.getResourceAsStream() [英] Cant retrieve an images from the WEB-INF folder ussing classLoader.getResourceAsStream()

查看:131
本文介绍了无法从WEB-INF文件夹中检索图像ussing classLoader.getResourceAsStream()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

中午举行,我试图让我的应用程序通过javamail发送两个HTML +图像,我只设法发送HTML,但与图像我有一些问题。我决定创建一个多部分消息,并且一切都很顺利,但是随后我使用类加载器从WEB-INF / resources / images中检索.png文件,我得到一个NullPointerExcetion,我不知道为什么?



这是我的EJB(3.0)的外观。我很欣赏这一款手,我对ClassLoader类没有多少经验(不太了解它)。

  @Stateless (name =ejbs / EmailServiceEJB)
public class EmailServiceEJB实现IEmailServiceEJB {

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

public void sendAccountActivationLinkToBuyer(String destinationEmail,
String name){

//电子邮件的目的地
String to = destinationEmail;
来自=dontreply2thismessage@gmail.com的字符串;

try {
Message message = new MimeMessage(mailSession);
//发件人:是我们的服务
message.setFrom(新的InternetAddress(从));
// To:给出
的目标message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(to));
message.setSubject(Uspijesna registracija);
//如何在http://www.rgagnon.com/javadetails/java-0321.html找到
message.setContent(generateActivationLinkTemplate(),text / html);

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

//准备一个多部分HTML
Multipart multipart = new MimeMultipart();
//准备HTML
BodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent(generateActivationLinkTemplate(),text / html);
multipart.addBodyPart(htmlPart);
//准备图像
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);
//设置消息内容!
message.setContent(multipart);

Transport.send(message);

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

}

我想提一提,我使用glassfishV3管理JEE6我不知道我的方法是否与此应用程序服务器兼容。






更新 b>
当我修改上面的代码到

  String fileName =logoemailtemplate.png; 

我收到一封电子邮件,但现在我没有收到文字。 :)有没有什么错误? 解决方案

我觉得你很混乱 ClassLoader#getResourceAsStream() 的ServletContext#的getResourceAsStream() 。前者只加载类路径中的资源,而后者只加载webcontent中的资源(也就是 / WEB-INF 文件夹所在的位置)。



您需要将这些资源放入类路径中。如果您使用的是IDE,那么最直接的方法就是将它们放在Java源文件夹中的任何包中。它会在build之后的 / WEB-INF / classes 结尾,这是classpath的一部分。



让我们假设你已经有一个包 com.example.resources.images ,并且你已经放弃了 logoemailtemplate.png 文件那么你可以通过下面的 fileName 来加载它。

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

另一种方法是添加 / WEB-INF / resources 文件夹到类路径。在像Eclipse这样的IDE中,您可以通过将其作为源文件夹添加到项目的构建路径中来实现。然后你可以通过下面的 fileName 来加载它。

  String fileName = /images/logoemailtemplate.png; 

然而,这并非常见做法。


The hold noon i was trying to make my app send both html+images via javamail, i only managed to send html, but with the image i am having a bit of problems. I decided to create a multipart message, and all was going ok, but then i use the class loader to retrieve the .png file from the WEB-INF/resources/images i get a NullPointerExcetion, i dont know why is that?

Here is how my EJB(3.0) looks like. Ill appreciate a hand on this one i dont have much experience with the ClassLoader class(Dont 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 am ussing JEE6 with glassfishV3 i dont know if my approach is compatible with this application server.


Update When i modify the above code to

String fileName = "logoemailtemplate.png";

I receive an email, it works.

But now I don't receive the text. :) Is there any mistake?

解决方案

I think you're confusing ClassLoader#getResourceAsStream() with ServletContext#getResourceAsStream(). The former only loads resources from the classpath while the later only loads resources from the webcontent (there where your /WEB-INF folder also is).

You need to put those resources in the classpath. If you're using an IDE, then most straightforward way is to just drop them in any package in the Java source folder. It'll end up in /WEB-INF/classes after build, which is part of the classpath.

Lets assume that you've a package com.example.resources.images and you've dropped logoemailtemplate.png file in there, then you can load it by the following fileName.

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

An alternative is to add /WEB-INF/resources folder to the classpath. In an IDE like Eclipse, you can do that by adding it as Source folder in project's build path. Then you can load it by the following fileName.

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

This is however not the common practice.

这篇关于无法从WEB-INF文件夹中检索图像ussing classLoader.getResourceAsStream()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆