2012-01-23 176 views
0

任何人都可以建議我如何使用Java保存電子郵件附件? 它是加密文件(PGP)。我需要保存並用於解密。我使用此代碼:保存電子郵件附件

String host = "mail.foxbox.lt"; 
String user = "user"; 
String password = "pass"; 
Properties properties = System.getProperties(); 

Session session = Session.getDefaultInstance(properties); 
Store store = session.getStore("pop3"); 
store.connect(host, user, password); 
Folder folder = store.getFolder("inbox"); 
folder.open(Folder.READ_WRITE); 

Message[] message = folder.getMessages(); 
for (int a = message.length-1; a < message.length; a++) { 
    Multipart multipart = (Multipart) message[a].getContent(); 
    for (int i = 0; i < multipart.getCount(); i++) { 
      BodyPart bodyPart = multipart.getBodyPart(i); 
      InputStream stream = bodyPart.getInputStream(); 
      if (Part.ATTACHMENT.equals(bodyPart.getDisposition())) { 
       BufferedReader br = new BufferedReader(new InputStreamReader(stream)); 
       FileWriter fstream = new FileWriter("AAA001.txt.pgp"); 
       BufferedWriter out = new BufferedWriter(fstream); 

       int y; 
       while ((y = stream.read()) != -1) { 
        out.write(y); 
       } 
       stream.close(); 
       out.close(); 
      } 
    } 
    System.out.println(); 
} 
folder.close(true); 
store.close(); 

但它只適用於文本文件(字符串)。在我的情況下它會改變.pgp文件並給我解密錯誤。 如何保存沒有任何流的文件? 謝謝。

回答

0

試試這個:

String host = "host"; 
String user = "user"; 
String password = "pass"; 
Properties properties = System.getProperties(); 
Session session = Session.getDefaultInstance(properties); 
Store store = session.getStore("pop3"); 
store.connect(host, user, password); 
Folder folder = store.getFolder("inbox"); 
folder.open(Folder.READ_WRITE); 
Message[] message = folder.getMessages(); 
for (int a = message.length-1; a < message.length; a++) { 
     Multipart multipart = (Multipart) message[a].getContent();  
    for (int i = 0; i < multipart.getCount(); i++) { 
     BodyPart bodyPart = multipart.getBodyPart(i);   
     InputStream is = bodyPart.getInputStream(); 
     File f = new File("/tmp/" + bodyPart.getFileName()); 
     FileOutputStream fos = new FileOutputStream(f); 
     byte[] buf = new byte[4096]; 
     int bytesRead; 
     while((bytesRead = is.read(buf))!=-1) { 
      fos.write(buf, 0, bytesRead); 
     } 
     fos.close(); 
     attachments.add(f); 
    } 
    } 
    folder.close(true); 
    store.close(); 
} 
3

的問題是,您使用BufferedReaderBufferedWriter。這些旨在用於字符流。將使用默認平臺編碼將您從附件讀取的字節序列轉換爲Unicode字符代碼點(char類型),反之亦然。在編碼或解碼過程中,無法映射的字符可能會被替換或從流中排除。

你必須使用純InputStream閱讀附件和FileOutputStream用於寫入文件:

InputStream stream = bodyPart.getInputStream(); 
if (Part.ATTACHMENT.equals(bodyPart.getDisposition())) { 
    FileOutputStream fstream = new FileOutputStream("AAA001.txt.pgp"); 

    byte[] buffer = new byte[1024]; 
    int len; 
    while ((len = stream.read(buffer)) != -1) { 
     fstream.write(buffer, 0, len); 
    } 

    fstream.close(); 
} 
stream.close(); 

還有其他的方法來複制一個InputStream內容爲OutputStream,看到this question and the answers

+0

謝謝:)快速回答+清楚的解釋+代碼!完善! – user1164545

+0

@ user1164545你喜歡這個答案?所以請注意並標記爲正確的答案... – m0skit0