即使调用file.delete()方法,图像也不会被删除

时间:2022-12-04 20:21:03

I need to send an email along with an embedded image. Once the email has been sent, the image in the application server should be deleted immediately. The problem I'm facing is, after the email is sent, the control goes method which contain,

我需要发送一封电子邮件和一个嵌入式图像。电子邮件发送后,应立即删除应用程序服务器中的图像。我面临的问题是,在发送电子邮件之后,控件进入包含的方法,

File file = new File("../bar.jpeg")
if(file.exists()){
  file.delete();
  System.out.println("Barcode Image Deleted");
}

It's printing "Barcode Image Deleted". But, the image is not deleted and is still present in the same location. I'm using multipart to attach the image to the email.

它正在打印“条形码图像已删除”。但是,图像不会被删除,并且仍然存在于同一位置。我正在使用multipart将图像附加到电子邮件中。

Why is it not getting deleted?

为什么不删除?

4 个解决方案

#1


Are you using javax.mail?
If so, you'll need to wait till the mail has finished being sent, which you find out about by registering a TransportListener.

你在用javax.mail吗?如果是这样,您需要等到邮件发送完毕,您可以通过注册TransportListener找到它。

This also means you won't be able to use the static Transport.send() methods, but will have to construct and clean up your own session and transport.

这也意味着您将无法使用静态Transport.send()方法,但必须构建和清理自己的会话和传输。

I'm trying to remember details from a while ago... I think the DataHandler or DataSource don't close the input stream when they've finished reading it, so you need to keep a reference to it and close it yourself before you can delete the underlying file.

我正在尝试记住前一段时间的细节...我认为DataHandler或DataSource在读完输入流后不会关闭输入流,所以你需要保留对它的引用并自己关闭它可以删除基础文件。

#2


The File.delete method returns a boolean which indicates whether the deletion was successful.

File.delete方法返回一个布尔值,指示删除是否成功。

It could be that the file deletion is not being successfully performed due to not having permissions to delete the file.

由于没有删除文件的权限,可能是文件删除未成功执行。

#3


File.delete() returns a true/false condition. Try checking the return condition of delete to see if the system is actually reporting the file as deleted.

File.delete()返回true / false条件。尝试检查删除的返回条件,以查看系统是否实际将文件报告为已删除。

#4


Firstly, File.delete() returns a boolean if it successfully deletes a file. Check that value and at least log it.

首先,如果成功删除文件,File.delete()将返回一个布尔值。检查该值并至少记录它。

If it is not being deleted, then I would guess that either

如果它没有被删除,那么我猜也是

  1. the file is currently open for reading and the OS won't let you delete it until it is closed. Possible your mail software? My guess is that the mail software does not try to base64 encode the image (for inclusion in the message) until it is actually sending the message...and/or does not stop reading it until the mail is sent.
  2. 该文件当前已打开以供阅读,操作系统将不允许您在关闭之前将其删除。可能你的邮件软件?我的猜测是,邮件软件不会尝试对图像进行base64编码(包含在邮件中),直到它实际发送邮件...和/或在邮件发送之前不会停止读取。

  3. the java process does not have permissions to delete the file
  4. java进程没有删除文件的权限

#1


Are you using javax.mail?
If so, you'll need to wait till the mail has finished being sent, which you find out about by registering a TransportListener.

你在用javax.mail吗?如果是这样,您需要等到邮件发送完毕,您可以通过注册TransportListener找到它。

This also means you won't be able to use the static Transport.send() methods, but will have to construct and clean up your own session and transport.

这也意味着您将无法使用静态Transport.send()方法,但必须构建和清理自己的会话和传输。

I'm trying to remember details from a while ago... I think the DataHandler or DataSource don't close the input stream when they've finished reading it, so you need to keep a reference to it and close it yourself before you can delete the underlying file.

我正在尝试记住前一段时间的细节...我认为DataHandler或DataSource在读完输入流后不会关闭输入流,所以你需要保留对它的引用并自己关闭它可以删除基础文件。

#2


The File.delete method returns a boolean which indicates whether the deletion was successful.

File.delete方法返回一个布尔值,指示删除是否成功。

It could be that the file deletion is not being successfully performed due to not having permissions to delete the file.

由于没有删除文件的权限,可能是文件删除未成功执行。

#3


File.delete() returns a true/false condition. Try checking the return condition of delete to see if the system is actually reporting the file as deleted.

File.delete()返回true / false条件。尝试检查删除的返回条件,以查看系统是否实际将文件报告为已删除。

#4


Firstly, File.delete() returns a boolean if it successfully deletes a file. Check that value and at least log it.

首先,如果成功删除文件,File.delete()将返回一个布尔值。检查该值并至少记录它。

If it is not being deleted, then I would guess that either

如果它没有被删除,那么我猜也是

  1. the file is currently open for reading and the OS won't let you delete it until it is closed. Possible your mail software? My guess is that the mail software does not try to base64 encode the image (for inclusion in the message) until it is actually sending the message...and/or does not stop reading it until the mail is sent.
  2. 该文件当前已打开以供阅读,操作系统将不允许您在关闭之前将其删除。可能你的邮件软件?我的猜测是,邮件软件不会尝试对图像进行base64编码(包含在邮件中),直到它实际发送邮件...和/或在邮件发送之前不会停止读取。

  3. the java process does not have permissions to delete the file
  4. java进程没有删除文件的权限