gpt4 book ai didi

gmail - JavaMail BaseEncode64 错误

转载 作者:行者123 更新时间:2023-12-04 13:55:48 25 4
gpt4 key购买 nike

我目前正在开发一个从 gmail 帐户下载附件的应用程序。
现在,每当下载压缩附件时我都会出错。但是,不是全部,有些我可以毫无错误地检索它。这是异常消息:

Exception in thread "main" com.sun.mail.util.DecodingException: BASE64Decoder: Error in encoded stream: needed 4 valid base64 characters but only got 1 before EOF, the 10 most recent characters were: "Q3w5ilxj2P"

仅供引用:我能够通过 gmail 网络界面下载附件。

这是片段:
        Multipart multipart = (Multipart) message.getContent();

for (int i = 0; i < multipart.getCount(); i++) {

BodyPart bodyPart = multipart.getBodyPart(i);

if (bodyPart.getFileName().toLowerCase().endsWith("zip") ||
bodyPart.getFileName().toLowerCase().endsWith("rar")) {
InputStream is = bodyPart.getInputStream();
File f = new File("/tmp/" + bodyPart.getFileName());
FileOutputStream fos = new FileOutputStream(f);
byte[] buf = new byte[bodyPart.getSize()];
int bytesRead;
while ((bytesRead = is.read(buf)) != -1) {
fos.write(buf, 0, bytesRead);
}
fos.close();
}
}
}

任何人都有想法,如何解决这个问题?

最佳答案

来自 known limitations, bugs, issues 的列表JavaMail:

Certain IMAP servers do not implement the IMAP Partial FETCH functionality properly. This problem typically manifests as corrupt email attachments when downloading large messages from the IMAP server. To workaround this server bug, set the "mail.imap.partialfetch" property to false. You'll have to set this property in the Properties object that you provide to your Session.



所以你应该只是 关闭 在 imap session 中部分获取。例如:
Properties props = System.getProperties();
props.setProperty("mail.store.protocol", "imap");
props.setProperty("mail.imap.partialfetch", "false");
Session session = Session.getDefaultInstance(props, null);
Store store = session.getStore("imaps");
store.connect("imap.gmail.com", "<username>","<password>");

来源: https://javaee.github.io/javamail/docs/api/com/sun/mail/imap/package-summary.html

关于gmail - JavaMail BaseEncode64 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1755414/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com