title: Unable to download PDF attachment with java by using JavaMail
body: <p>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.</p>
  
  <p>Now problem comes when it comes to download "PDF Attachments". PDF files are not downloading completely... it is missing some contains.</p>
  
  <p>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..."</p>
  
  <p>Here is code I have written to download attachment:</p>
  
  <pre><code>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;
   }
  </code></pre>
  
  <p>Am I missing something? Any useful help is appreciated.</p>