1
protected void executeInternal(JobExecutionContext context) throws JobExecutionException 
{
  System.out.println("Sending Birthday Wishes... ");

   try
   {
            for(int i=0;i<maillist.length;i++)
            {   


            Email email = new Email();
            email.setFrom("spv_it@yahoo.com");
            email.setSubject("Happy IndependenceDay");
            email.setTo(maillist[i]);

            email.setText("<font color=blue><h4>Dear Users,<br><br><br>Wish you a Happy Independence Day!<br><br><br>Regards,<br>Penna Cement Industries Limited</h4></font>");
            byte[] data = null;
            ClassPathResource img = new ClassPathResource("newLogo.gif");
            InputStream inputStream = img.getInputStream();
            data = new byte[inputStream.available()];
            while((inputStream.read(data)!=-1));

            Attachment attachment = new Attachment(data, "HappyBirthDay","image/gif", true);
            email.addAttachment(attachment);

            emailService.sendEmail(email);
        }


   }
   catch (MessagingException e)
   {
    e.printStackTrace();
   }
   catch (Exception e)
   {
    e.printStackTrace();
   }

 }

这是我得到的错误:

java.io.FileNotFoundException: class path resource [newLogo.gif] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:135)
at com.mail.schedular.BirthdayWisherJob.executeInternal(BirthdayWisherJob.java:55)
at org.springframework.scheduling.quartz.QuartzJobBean.execute(QuartzJobBean.java:66)
at org.quartz.core.JobRunShell.run(JobRunShell.java:223)
at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:549)
4

3 回答 3

0

最佳做法是通过提及该文件的 ABSOLUTE PATH来读取/写入或提供任何文件的参考。对于您的问题,它显示了FileNotFoundException,因为 JVM 未能在您的当前目录中找到该文件,默认情况下该目录是您的源路径。因此,请在ClassPathResource中提供绝对路径或将该图像文件复制到您的当前目录。它会解决你的问题。

于 2013-08-16T06:52:43.823 回答
0

我认为您需要将文件放在 src 文件夹中,如果存在则检查它是否在 src 目录中的某个目录下。

然后给出正确的位置,如 src[dir]----->newLogo.gif 下面给出的详细信息

ClassPathResource img = new ClassPathResource("newLogo.gif");

或者,src[dir]----->images[dir]---->newLogo.gif

ClassPathResource img = new ClassPathResource("/images/newLogo.gif");
于 2013-08-16T07:26:56.373 回答
0

由于作业在单独的石英线程中运行,因此出现此错误,我建议您在 jar 之外找到文件newLogo.gif并使用以下内容加载它。 Thread.currentThread().getContextClassLoader().getResource("classpath:image/newLogo.gif");

于 2020-10-22T13:45:50.363 回答