繁体   English   中英

如何从项目资源文件夹获取图像路径并发送到Java Eclipse中的另一个类

[英]How to get images path from project resource folder and send to another class in java eclipse

在我的Java Web应用程序中,我需要访问图像中项目资源的路径并将其发送到另一个sendmail.java类。我如何从资源文件夹中获取图像路径?有人可以告诉我吗?

我的图片文件夹:

图像文件夹结构

当我尝试使用以下代码时,它显示file not found error

我尝试了这个:

String imgpath="/resources/HappyBirthday.JPG";
SendEmail stp=new SendEmail();
stp.mail(From, To,Name,text,imgpath);

SendEmail.java:

public String mail(String From,String To,String Name,String text,String imgPath){
Properties props = new Properties();
          props.put("mail.smtp.host", "mail.com");
          props.put("mail.smtp.socketFactory.port", "465");
          props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
          props.put("mail.smtp.auth", "false");
          props.put("mail.smtp.port", "25");
          // Get the default Session object.
          Session session = Session.getDefaultInstance(props);
 try {            
             MimeMessage message = new MimeMessage(session);
             message.setFrom(new InternetAddress(From));
             message.addRecipient(Message.RecipientType.TO, new InternetAddress(To));
             message.setSubject("Wishes!", "UTF-8");
             message.setText(text, "UTF-8");

          // first part (the html)
              BodyPart messageBodyPart = new MimeBodyPart();
              String htmlText = "<H1>Hello</H1><img src=\"cid:image\">";
              messageBodyPart.setContent(htmlText, "text/html");
              // add it

            // second part (the image)
              messageBodyPart = new MimeBodyPart();         
              DataSource fds = new FileDataSource(imgPath); //here adding image path to send mail like image and text 
              messageBodyPart.setDataHandler(new DataHandler(fds));
              messageBodyPart.setHeader("Content-ID", "<image>");           
             MimeMultipart multipart = new MimeMultipart("related");
             multipart.addBodyPart(messageBodyPart);
             message.setContent(multipart);
             // Send message
             Transport.send(message);
             System.out.println("Sent message successfully....");          
          }catch (MessagingException mex) {
             mex.printStackTrace();
             System.out.println(mex);
          }
}    

谢谢

期望文件不正确。 如果您的代码在jar中编译,则该文件不可用。

改为使用

InputStream inputStream = this.getClass().getResourceAsStream("/HappyBirthday.JPG");

ByteArrayDataSource ds = new ByteArrayDataSource(inputStream, "image/jpg");

编译代码后,资源将移到classes并且可以作为资源流而不是文件进行访问。

如果使用Maven,则应将其添加到pom.xml中

<resources>
        <resource>
            <directory>${basedir}/resources</directory>
            <includes>
                <include>**/*</include>
            </includes>
        </resource>
    </resources>

然后它将与

String imgpath="HappyBirthday.JPG";

很抱歉,如果我对Maven错误,仍然无法对问题发表评论

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM