任何人都可以建議我如何使用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文件並給我解密錯誤。 如何保存沒有任何流的文件? 謝謝。
謝謝:)快速回答+清楚的解釋+代碼!完善! – user1164545
@ user1164545你喜歡這個答案?所以請注意並標記爲正確的答案... – m0skit0