开发者

How to decode a string on runtime in java?

开发者 https://www.devze.com 2022-12-18 06:12 出处:网络
I\'m creating email client, when i receive emails from blackberry server it sends file name as \"=?utf-8?B?anBlZ2F0dGFjaG1lbnQuSlBFRw==?=\" but the original filename was \"jpegattachment.JPEG\", and s

I'm creating email client, when i receive emails from blackberry server it sends file name as "=?utf-8?B?anBlZ2F0dGFjaG1lbnQuSlBFRw==?=" but the original filename was "jpegattachment.JPEG", and sometime I get the plain text when I receive from other mail servers. So here开发者_如何学Go my problem is I can get a string which may or may not be encoded.

Is there any way, I can get the encoding of string and decode that into plain text.

Either the input string is "=?utf-8?B?anBlZ2F0dGFjaG1lbnQuSlBFRw==?=" or "jpegattachment.JPEG" output should be "jpegattachment.JPEG".

Any Idea??


This is MIME-encoded. Even though Base64 is most popular, it may use other encodings like Quoted-printable, binary etc. So you should use an existing library to decode this. Any mail program will have decoder built-in.

You can use the decodeWord() from Java Mail,

http://java.sun.com/products/javamail/javadocs/javax/mail/internet/MimeUtility.html#decodeWord(java.lang.String)

Try deocdeText() if you want leave plain-text alone.


Yes, this: anBlZ2F0dGFjaG1lbnQuSlBFRw== is base64 encoded jpegattachment.JPEG. So just decode it with base64.

Answered how, here: Decode Base64 data in Java

0

精彩评论

暂无评论...
验证码 换一张
取 消