关于MIME 和Base64

MIME: Multipurpose Internet Email Extension 多功能Internet 邮件扩充服务

MIME原来是同SMTP协议来起应用来,后来广泛应用于http等中。

其显著的特点是所有的内容用US-ASCII来显示,一个完整的MIME消息如下:

MIME-version: 1.0
Content-type: multipart/mixed; boundary="frontier"

This is a message with multiple parts in MIME format.
--frontier
Content-type: text/plain

This is the body of the message.
--frontier
Content-type: text/plain

another message
--frontier
Content-type: application/octet-stream
Content-transfer-encoding: base64

PGh0bWw+CiAgPGhlYWQ+CiAgPC9oZWFkPgogIDxib2R5PgogICAgPHA+VGhpcyBpcyB0aGUg
Ym9keSBvZiB0aGUgbWVzc2FnZS48L3A+CiAgPC9ib2R5Pgo8L2h0bWw+Cg==
--frontier--


它包含一个头和内容体,内容体又可包含多个message, 每个消息有个消息头和消息体。

对于多媒体文件附加上消息加,一般可设置头Content-transfer-encoding为base64编码方式,将二进制流转为字条串方式。

------------------------------------------------------
生成一个MIME文档的例子:
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;

public class MyTest {
    public static void main(String[] args){
        try{
            MimeMultipart mult=new MimeMultipart();
            MimeBodyPart part1=new MimeBodyPart();
            MimeBodyPart part3=new MimeBodyPart();
            part1.setText("abc");
            MimeBodyPart part2=new MimeBodyPart();
            part2.setText("cdef<aaaa>");
            mult.addBodyPart(part1);
            mult.addBodyPart(part2);
            //System.out.println(mult.toString());

            FileDataSource   source=new   FileDataSource("d:\\temp\\log.jpg");  
            part3.setDataHandler(new   DataHandler(source));  
            part3.setFileName(source.getName());               
            part3.setHeader("Content-Transfer-Encoding", "base64");
            
            mult.addBodyPart(part3);
            mult.writeTo(System.out);
            System.out.println("------part3 is "+part3.getEncoding());
            
            System.out.println("-----encode source is : " + MimeUtility.getEncoding(source));
            
        }catch (Exception e){
            e.printStackTrace();
        }
            
    }
}</aaaa>


-------------------------------------
Base64指用 a-z、A-Z、0-9 62个基础字符及 "+", "-","/","=" 中的几个 对进制流进行编码,
编码方法是将原来的三个字节变成6*4的方式四个字节, 6位二进制就是64个字符。

-----------------------------------

参考: http://en.wikipedia.org/wiki/MIME

你可能感兴趣的:(base64)