我正在使用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");
} …Run Code Online (Sandbox Code Playgroud)