简体   繁体   中英

How to displaly progress bar for downloading attachment

I display the progress bar for downloading attachments and it works fine … but when I am downloading some attachments I get the exception message:

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"

I found the explanation:

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.

http://java.sun.com/products/javamail/NOTES113.txt

So I turned off partial fetch:

Properties props = System.getProperties();
props.setProperty("mail.store.protocol", "imaps");
props.setProperty("mail.imaps.partialfetch", "false");
Session session = Session.getDefaultInstance(props, null);
Store store = session.getStore("imaps");
store.connect("imap.gmail.com", "<username>","<password>");

this solved the problem ….however the method getInputStream() from the Part class blocks the thread until the attachment is completely downloaded and it is impossible to get the information about the number of bytes which have been already downloaded from mailbox. Without this information it is impossible to display the progress bar. So is there a way to obtain this information and display the progress bar?

It's difficult.

The only approach I know of is to create your own socket factory that produces a special kind of socket (of your own design) that returns an InputStream that wraps the real socket InputStream and allows you to monitor the amount of data read through the stream. This will allow you to measure the IMAP protocol data that is being returned from the server to the client. It won't be able to tell you what percentage of the data has been returned (because the data you're measuring includes all the IMAP protocol overhead and encoding overhead), but it should allow you to see that progress is being made.

If you create something like this, I'm sure others would be very interested in it!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM