无法使用 JavaMail 通过 java 下载 PDF 附件
我正在使用 JavaMail API 创建电子邮件客户端。一切工作正常,就像我能够连接到邮件服务器(使用 IMAP)、删除邮件、检索收到的邮件并将其显示给用户等。
现在下载“PDF 附件”时出现问题。 PDF 文件未完全下载...它缺少一些内容。
如果当我使用 IE 或任何其他 Web 浏览器下载附件时,某些 PDF 附件的大小为 38 Kb,但当我使用 java 代码下载它时,它的大小为 37.3 Kb。它并不完整 因此,当我尝试使用 Adobe Reader 打开它时,它会显示错误消息“文件已损坏...”
这是我编写的用于下载附件的代码:
public boolean saveFile(String filename,Part part) throws IOException, MessagingException {
boolean ren = true;
FileOutputStream fos = null;
BufferedInputStream fin = null;
InputStream input = part.getInputStream();
File pdffile = new File("d:/"+filename);
try{
if(!pdffile.exists()){
fos = new FileOutputStream(pdffile);
fin = new BufferedInputStream(input);
int size = 512;
byte[] buf = new byte[size];
int len;
while ( (len = fin.read(buf)) != -1 ) {
fos.write(buf, 0, len);
}
input.close();
fos.close();
}else{
System.out.println("File already exists");
}
}catch(Exception e ){
ren = false;
}
return ren;
}
我是否缺少某些内容?任何有用的帮助表示赞赏。
I am creating Email client using JavaMail API. Everything is working fine like I am able to connect to mail server(using IMAP), Delete mail, retrieving received mails and displaying them to user etc.
Now problem comes when it comes to download "PDF Attachments". PDF files are not downloading completely... it is missing some contains.
If some PDF attachment is of size 38 Kb when I am downloading attachment using IE or any other web browser but when I am downloading it using my java code it is of size 37.3 Kb. It is not complete
Hence when I try to open it using Adobe Reader it shows error message that "File is corrupted..."
Here is code I have written to download attachment:
public boolean saveFile(String filename,Part part) throws IOException, MessagingException {
boolean ren = true;
FileOutputStream fos = null;
BufferedInputStream fin = null;
InputStream input = part.getInputStream();
File pdffile = new File("d:/"+filename);
try{
if(!pdffile.exists()){
fos = new FileOutputStream(pdffile);
fin = new BufferedInputStream(input);
int size = 512;
byte[] buf = new byte[size];
int len;
while ( (len = fin.read(buf)) != -1 ) {
fos.write(buf, 0, len);
}
input.close();
fos.close();
}else{
System.out.println("File already exists");
}
}catch(Exception e ){
ren = false;
}
return ren;
}
Am I missing something? Any useful help is appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
花了几个小时研究这个问题,终于弄清楚了。
为我做的。与上面的 @Shantanu 几乎相同,但因为我正在使用,
所以我也需要使用“imaps”进行部分获取。
就像魅力一样。
完整代码如下:
...
Spent a few hours on this and finally figured it out.
did it for me. Almost the same thing as @Shantanu above, but because I was using
I needed to use "imaps" for the partialfetch as well.
Works like a charm.
Full code below:
...
最后我在 JavaMail FAQ Reading Mail, IMAP 部分找到了解决方案
Gmail 服务器运行附件错误
首先我尝试将partialfetch 属性设置为 false,但有时有效有时无效常见
问题解答中列出了另一种方法,即使用 MimeMessage 的复制构造函数并将原始对象存储在某些 tempmsg 中,然后获取内容tempmsg
并现在执行它应该工作的所有操作。
有关实际发生情况的详细信息,请转到 JavaMail 常见问题解答 阅读 Mail、IMAP 部分您将找到所有答案。
Finally I found solution at JavaMail FAQ Reading Mail, IMAP section
Gmail server is running bug with attachments
First I tried to set partialfetch property false but sometimes it works sometimes it doesn't
There is another way listed in FAQ which is just use copy constructor of MimeMessage and store orignal object in some tempmsg and then get content of tempmsg
and now perform all operations it should work..
For detailed information about what actually happens goto JavaMail FAQ Reading Mail, IMAP section you will find all answers..