<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>JavaChen Blog &#187; MIME</title>
	<atom:link href="http://www.javachen.com/tag/mime/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.javachen.com</link>
	<description>Just some random thoughts about technology,Java and life</description>
	<lastBuildDate>Mon, 06 Sep 2010 14:10:42 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
	
<!-- Start Of Script Generated By WP-PostViews Plus -->
<script type='text/javascript' src='http://www.javachen.com/wp-includes/js/jquery/jquery.js?ver=1.4.2'></script>
<script type="text/javascript">
/* <![CDATA[ */
jQuery.ajax({type:'GET',url:'http://www.javachen.com/wp-content/plugins/wp-postviews-plus/postviews_plus.php',data:'todowppvp=add&type=tag&id=120_1',cache:false,dataType:'script'});
/* ]]> */
</script>
<!-- End Of Script Generated By WP-PostViews Plus -->
	<item>
		<title>What is Base64?</title>
		<link>http://www.javachen.com/2010/01/what-is-base64/</link>
		<comments>http://www.javachen.com/2010/01/what-is-base64/#comments</comments>
		<pubDate>Tue, 26 Jan 2010 10:00:46 +0000</pubDate>
		<dc:creator>JavaChen</dc:creator>
				<category><![CDATA[C/Algorithm]]></category>
		<category><![CDATA[Base64]]></category>
		<category><![CDATA[http]]></category>
		<category><![CDATA[MIME]]></category>

		<guid isPermaLink="false">http://www.javachen.com/?p=912</guid>
		<description><![CDATA[这是一篇普及互联网术语的文章。每天呆在互联网上，发现呆的时间越长，发现自己不知道的东西越多。互联网上有太多的知识，虽然我们不可能一下子了解透彻， 但是遇到一个就有必要去了解其相关的知识或是原理，这样以后再见到他就不会觉得陌生了。 Base64是一种使用64基的位置计数法。它使用2的最大次方来代表仅可打印的ASCII 字符。这使它可用来作为电子邮件的传输编码。在Base64中的变量使用字符A-Z、a-z和0-9 ，这样共有62个字符，用来作为开始的64个数字，最后两个用来作为数字的符号在不同的系统中而不同。例如：&#8221;ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/&#8220;。 base64是一种将二进制的01序列转化成ASCII字符的编码方法。编码后的文本或者二进制消息，就可以运用SMTP等只支持ASCII字符的协议传送了。Base64一般被认为会平均增加33%的报文长度，而且，经过编码的消息对于人类来说是不可读的。 在MIME格式的电子邮件中，base64可以用来将binary的字节序列数据编码成ASCII字符序列构成的文本。 算法详解 Base64要求把每三个8Bit的字节转换为四个6Bit的字节（3*8 = 4*6 = 24），然后把6Bit再添两位高位0，组成四个8Bit的字节，也就是说，转换后的字符串理论上将要比原来的长1/3。 转换的时候，将三个byte的数据，先后放入一个24bit的缓冲区中，先来的byte占高位。数据不足3byte的话，于缓冲区中剩下的bit用 0补足。然后，每次取出6（因为26 = 64）个bit，按照其值选择ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/中的字符作为编码后的输出。不断进行，直到全部输入数据转换完成。 如果最后剩下两个输入数据，在编码结果后加1个“=”；如果最后剩下一个输入数据，编码结果后加2个“=”；如果没有剩下任何数据，就什么都不要加，这样才可以保证资料还原的正确性。 注意：根据RFC822规定，每76个字符，还需要加上一个回车换行。 转换后，我们用一个码表来得到我们想要的字符串（也就是最终的Base64编码），这个表是这样的：（摘自RFC2045） Table 1: The Base64 Alphabet Value Encoding  Value Encoding  Value Encoding  Value Encoding 0 A            17 R            34 i            51 z 1 B            18 S            35 j            52 0 2 C            19 T            36 k            53 1 3 D            20 U            37 l            54 2 4 E            21 V            38 m            55 3 [...]]]></description>
			<content:encoded><![CDATA[<p>这是一篇普及互联网术语的文章。每天呆在互联网上，发现呆的时间越长，发现自己不知道的东西越多。互联网上有太多的知识，虽然我们不可能一下子了解透彻， 但是遇到一个就有必要去了解其相关的知识或是原理，这样以后再见到他就不会觉得陌生了。</p>
<p><span style="color: #ff00ff;">Base64是一种使用64基的位置计数法</span>。它使用2的最大次方来代表仅可打印的ASCII 字符。这使它可用来作为电子邮件的传输编码。在Base64中的变量使用字符A-Z、a-z和0-9 ，这样共有62个字符，用来作为开始的64个数字，最后两个用来作为数字的符号在不同的系统中而不同。例如：&#8221;<span style="color: #0000ff;">ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/</span>&#8220;。<br />
<span style="color: #ff00ff;">base64是一种将二进制的01序列转化成ASCII字符的编码方法</span>。编码后的文本或者二进制消息，就可以运用SMTP等只支持ASCII字符的协议传送了。Base64一般被认为会平均增加<span style="color: #0000ff;">33%</span>的报文长度，而且，经过编码的消息对于人类来说是不可读的。<br />
在<a href="http://www.javachen.com/2010/01/what-is-mime%EF%BC%9F/" target="_blank">MIME</a>格式的电子邮件中，base64可以用来将binary的字节序列数据编码成ASCII字符序列构成的文本。<span id="more-912"></span></p>
<p>算法详解<br />
<span style="color: #0000ff;">Base64要求把每三个8Bit的字节转换为四个6Bit的字节（3*8 = 4*6 =  24），然后把6Bit再添两位高位0，组成四个8Bit的字节，也就是说，转换后的字符串理论上将要比原来的长1/3。</span><br />
转换的时候，将三个byte的数据，先后放入一个24bit的缓冲区中，先来的byte占高位。数据不足3byte的话，于缓冲区中剩下的bit用 0补足。然后，每次取出6（因为26 = 64）个bit，按照其值选择ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/中的字符作为编码后的输出。不断进行，直到全部输入数据转换完成。<br />
如果最后剩下两个输入数据，在编码结果后加1个“=”；如果最后剩下一个输入数据，编码结果后加2个“=”；如果没有剩下任何数据，就什么都不要加，这样才可以保证资料还原的正确性。</p>
<p><span style="color: #ff0000;">注意：根据RFC822规定，每76个字符，还需要加上一个回车换行。</span></p>
<p>转换后，我们用一个码表来得到我们想要的字符串（也就是最终的Base64编码），这个表是这样的：（摘自RFC2045）<br />
Table 1: The Base64 Alphabet</p>
<p>Value Encoding  Value Encoding  Value Encoding  Value Encoding<br />
0 A            17 R            34 i            51 z<br />
1 B            18 S            35 j            52 0<br />
2 C            19 T            36 k            53 1<br />
3 D            20 U            37 l            54 2<br />
4 E            21 V            38 m            55 3<br />
5 F            22 W            39 n            56 4<br />
6 G            23 X            40 o            57 5<br />
7 H            24 Y            41 p            58 6<br />
8 I            25 Z            42 q            59 7<br />
9 J            26 a            43 r            60 8<br />
10 K            27 b            44 s            61 9<br />
11 L            28 c            45 t            62 +<br />
12 M            29 d            46 u            63 /<br />
13 N            30 e            47 v<br />
14 O            31 f            48 w         (pad) =<br />
15 P            32 g            49 x<br />
16 Q            33 h            50 y</p>
<p>这也是Base64名称的由来，而Base64编码的结果不是根据算法把编码变为高两位是0而低6为代表数据，而是变为了上表的形式， 如”A”就有7位，而”a”就只有6位。表中，编码的编号对应的是得出的新字节的十进制值。</p>
<p>用更接近于编程的思维来说，编码的过程是这样的：<br />
第一个字符通过右移2位获得第一个目标字符的Base64表位置，根据这个数值取到表上 相应的字符，就是第一个目标字符。<br />
然后将第一个字符左移4位加上第二个字符右移4位，即获得第二个目标字符。<br />
再将第二个字符左移2位加上 第三个字符右移6位，获得第三个目标字符。<br />
最后取第三个字符的右6位即获得第四个目标字符。</p>
<p>在以上的每一个步骤之后，再把结果与 0x3F 进行 AND 位操作，就可以得到编码后的字符了。</p>
<p>在JAVA中要实现Base64的编码和解码是非常容易的，因为JDK中已经有提供有现成的类：<br />
    编码：<br />
String src =&#8221;BASE64编码测试&#8221;;<br />
sun.misc.BASE64Encoder en = new sun.misc.BASE64Encoder();<br />
String encodeStr = en.encode(src.getBytes());<br />
注意：当encodeStr的长度超过76时，会包含有回车和换行符</p>
<p>    解码：<br />
sun.misc.BASE64Decoder dec = new sun.misc.BASE64Decoder();<br />
byte[] data = dec.decodeBuffer(decodeStr);</p>
<p>最近在研究Xmappr项目时候，发现了其对Base64编码的一种实现：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">/*
 * This software is released under the BSD license. Full license available at http://xmappr.googlecode.com
 *
 * Copyright (c) 2008, 2009, Peter Knego &amp; Xmappr contributors
 * All rights reserved.
 */</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">xmappr.org.xmappr.converters</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
<span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * Utility class implementing Base64 decoder/encoder.
 */</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Base64 <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">char</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> base64code <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/&quot;</span>
			.<span style="color: #006633;">toCharArray</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// 初始化数组</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> nibbles <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">128</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">static</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #008000; font-style: italic; font-weight: bold;">/**
		 * The Base64 Alphabet
			Value Encoding 
			0 A 17 R 34 i 51 z
			1 B 18 S 35 j 52 0
			2 C 19 T 36 k 53 1
			3 D 20 U 37 l 54 2
			4 E 21 V 38 m 55 3
			5 F 22 W 39 n 56 4
			6 G 23 X 40 o 57 5
			7 H 24 Y 41 p 58 6
			8 I 25 Z 42 q 59 7
			9 J 26 a 43 r 60 8
			10 K 27 b 44 s 61 9
			11 L 28 c 45 t 62 +
			12 M 29 d 46 u 63 /
			13 N 30 e 47 v (pad) =
			14 O 31 f 48 w
			15 P 32 g 49 x
			16 Q 33 h 50 y 
		 */</span>
		<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> nibbles.<span style="color: #006633;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
			nibbles<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
		<span style="color: #666666; font-style: italic;">//将0-63存入nibbles['A']-nibbles['=']，生成上面的Base64位字母表</span>
		<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">64</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			nibbles<span style="color: #009900;">&#91;</span>base64code<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#41;</span> i<span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #003399;">String</span> encode<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> input<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> encode<span style="color: #009900;">&#40;</span>input, <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #003399;">String</span> encode<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> input, <span style="color: #000066; font-weight: bold;">int</span> lineLength<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
		<span style="color: #000066; font-weight: bold;">int</span> ilen <span style="color: #339933;">=</span> input.<span style="color: #006633;">length</span><span style="color: #339933;">;</span>
		<span style="color: #666666; font-style: italic;">// 计算加入多少个等于号到编码后面</span>
		<span style="color: #000066; font-weight: bold;">int</span> padding <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">3</span> <span style="color: #339933;">-</span> <span style="color: #009900;">&#40;</span>ilen <span style="color: #339933;">%</span> <span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">%</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000066; font-weight: bold;">char</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> encoded <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">char</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">4</span> <span style="color: #339933;">*</span> <span style="color: #009900;">&#40;</span>ilen <span style="color: #339933;">+</span> padding<span style="color: #009900;">&#41;</span> <span style="color: #339933;">/</span> <span style="color: #cc66cc;">3</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">int</span> c <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">int</span> j, j0, j1, j2<span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">&lt;</span> ilen<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			j0 <span style="color: #339933;">=</span> input<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
			j1 <span style="color: #339933;">=</span> i <span style="color: #339933;">&lt;</span> ilen <span style="color: #339933;">?</span> input<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">:</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
			j2 <span style="color: #339933;">=</span> i <span style="color: #339933;">&lt;</span> ilen <span style="color: #339933;">?</span> input<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">:</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
			j <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>j0 <span style="color: #339933;">&lt;&lt;</span> <span style="color: #cc66cc;">16</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span>j1 <span style="color: #339933;">&lt;&lt;</span> <span style="color: #cc66cc;">8</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> j2<span style="color: #339933;">;</span>
&nbsp;
			encoded<span style="color: #009900;">&#91;</span>c<span style="color: #339933;">++</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> base64code<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#40;</span>j <span style="color: #339933;">&gt;&gt;</span> <span style="color: #cc66cc;">18</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span> 0x3f<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
			encoded<span style="color: #009900;">&#91;</span>c<span style="color: #339933;">++</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> base64code<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#40;</span>j <span style="color: #339933;">&gt;&gt;</span> <span style="color: #cc66cc;">12</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span> 0x3f<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
			encoded<span style="color: #009900;">&#91;</span>c<span style="color: #339933;">++</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> base64code<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#40;</span>j <span style="color: #339933;">&gt;&gt;</span> <span style="color: #cc66cc;">6</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span> 0x3f<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
			encoded<span style="color: #009900;">&#91;</span>c<span style="color: #339933;">++</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> base64code<span style="color: #009900;">&#91;</span>j <span style="color: #339933;">&amp;</span> 0x3f<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// replace encoded padding nulls with &quot;=&quot;</span>
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>padding <span style="color: #339933;">==</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			encoded<span style="color: #009900;">&#91;</span>encoded.<span style="color: #006633;">length</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'='</span><span style="color: #339933;">;</span>
			encoded<span style="color: #009900;">&#91;</span>encoded.<span style="color: #006633;">length</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'='</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>padding <span style="color: #339933;">==</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			encoded<span style="color: #009900;">&#91;</span>encoded.<span style="color: #006633;">length</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'='</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">return</span> lineLength <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span> <span style="color: #339933;">?</span> splitLines<span style="color: #009900;">&#40;</span>encoded, lineLength<span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #003399;">String</span>
				.<span style="color: #006633;">valueOf</span><span style="color: #009900;">&#40;</span>encoded<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #003399;">String</span> splitLines<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">char</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> chars, <span style="color: #000066; font-weight: bold;">int</span> lineLength<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		StringBuilder lines <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> StringBuilder<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> chars.<span style="color: #006633;">length</span><span style="color: #339933;">;</span> i <span style="color: #339933;">+=</span> lineLength<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			lines.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span>chars, i, <span style="color: #003399;">Math</span>.<span style="color: #006633;">min</span><span style="color: #009900;">&#40;</span>chars.<span style="color: #006633;">length</span> <span style="color: #339933;">-</span> i, lineLength<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
					.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000000; font-weight: bold;">return</span> lines.<span style="color: #006633;">delete</span><span style="color: #009900;">&#40;</span>lines.<span style="color: #006633;">length</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">2</span>, lines.<span style="color: #006633;">length</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> decode<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> data<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> decode<span style="color: #009900;">&#40;</span>data.<span style="color: #006633;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">toCharArray</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> decode<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">char</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> chars<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
		<span style="color: #000066; font-weight: bold;">int</span> charCount <span style="color: #339933;">=</span> chars.<span style="color: #006633;">length</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// remove newlines from character count</span>
		<span style="color: #000066; font-weight: bold;">int</span> a <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>a <span style="color: #339933;">&lt;</span> chars.<span style="color: #006633;">length</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>chars<span style="color: #009900;">&#91;</span>a<span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'<span style="color: #000099; font-weight: bold;">\r</span>'</span> <span style="color: #339933;">||</span> chars<span style="color: #009900;">&#91;</span>a<span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: #009900;">&#41;</span>
				charCount<span style="color: #339933;">--;</span>
			a<span style="color: #339933;">++;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// check the char count</span>
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>charCount <span style="color: #339933;">%</span> <span style="color: #cc66cc;">4</span> <span style="color: #339933;">!=</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">IllegalArgumentException</span><span style="color: #009900;">&#40;</span>
					<span style="color: #0000ff;">&quot;Length of Base64 encoded input string is not a multiple of 4.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// count padding characters '='</span>
		<span style="color: #000066; font-weight: bold;">int</span> padCount <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>chars<span style="color: #009900;">&#91;</span>chars.<span style="color: #006633;">length</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">1</span> <span style="color: #339933;">-</span> padCount<span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'='</span><span style="color: #009900;">&#41;</span>
			padCount<span style="color: #339933;">++;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// create the output byte array, take padding into account</span>
		<span style="color: #000066; font-weight: bold;">int</span> olen <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">3</span> <span style="color: #339933;">*</span> charCount<span style="color: #009900;">&#41;</span> <span style="color: #339933;">/</span> <span style="color: #cc66cc;">4</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> padCount<span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> out <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#91;</span>olen<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">int</span> o <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">int</span> c <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> nibbleBlock <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">char</span> currentChar<span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">byte</span> currentNibble<span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">&lt;</span> chars.<span style="color: #006633;">length</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #666666; font-style: italic;">// read four character into temporary storage (and skip newlines)</span>
			c <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>c <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">4</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				currentChar <span style="color: #339933;">=</span> chars<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
				<span style="color: #666666; font-style: italic;">// skip newline and padding characters</span>
				<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>currentChar <span style="color: #339933;">!=</span> <span style="color: #0000ff;">'<span style="color: #000099; font-weight: bold;">\r</span>'</span> <span style="color: #339933;">&amp;&amp;</span> currentChar <span style="color: #339933;">!=</span> <span style="color: #0000ff;">'<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
					<span style="color: #666666; font-style: italic;">// nibble conversion table only holds 128 characters</span>
					<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>currentChar <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">127</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
						<span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">IllegalArgumentException</span><span style="color: #009900;">&#40;</span>
								<span style="color: #0000ff;">&quot;Illegal character in Base64 encoded data.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					<span style="color: #009900;">&#125;</span>
&nbsp;
					currentNibble <span style="color: #339933;">=</span> currentChar <span style="color: #339933;">!=</span> <span style="color: #0000ff;">'='</span> <span style="color: #339933;">?</span> nibbles<span style="color: #009900;">&#91;</span>currentChar<span style="color: #009900;">&#93;</span>
							<span style="color: #339933;">:</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
					<span style="color: #666666; font-style: italic;">// char does not exist (==-1) in conversion table</span>
					<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>currentNibble <span style="color: #339933;">==</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
						<span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">IllegalArgumentException</span><span style="color: #009900;">&#40;</span>
								<span style="color: #0000ff;">&quot;Illegal character in Base64 encoded data.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					<span style="color: #009900;">&#125;</span>
					nibbleBlock<span style="color: #009900;">&#91;</span>c<span style="color: #339933;">++</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> currentNibble<span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
			<span style="color: #009900;">&#125;</span>
&nbsp;
			<span style="color: #666666; font-style: italic;">// convert four nibbles into three bytes (4x 6-bit nibbles = 3</span>
			<span style="color: #666666; font-style: italic;">// bytes)</span>
			out<span style="color: #009900;">&#91;</span>o<span style="color: #339933;">++</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>nibbleBlock<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">&lt;</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">|</span> <span style="color: #009900;">&#40;</span>nibbleBlock<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">&gt;&gt;&gt;</span> <span style="color: #cc66cc;">4</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>o <span style="color: #339933;">&lt;</span> olen<span style="color: #009900;">&#41;</span>
				out<span style="color: #009900;">&#91;</span>o<span style="color: #339933;">++</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>nibbleBlock<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">&amp;</span> 0xf<span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;&lt;</span> <span style="color: #cc66cc;">4</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">|</span> <span style="color: #009900;">&#40;</span>nibbleBlock<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">&gt;&gt;&gt;</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>o <span style="color: #339933;">&lt;</span> olen<span style="color: #009900;">&#41;</span>
				out<span style="color: #009900;">&#91;</span>o<span style="color: #339933;">++</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>nibbleBlock<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">&amp;</span> <span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;&lt;</span> <span style="color: #cc66cc;">6</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">|</span> nibbleBlock<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">return</span> out<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p><span style="color: #ff00ff;">网上有很多介绍关于base64编码的例子，典型的是用“张3”举例得到的编码是“1iUz”，可是我用上面的代码得到的结果是：“1MUz”，两种结果相差了一位，很是奇怪。这是为什么？！</span></p>
<h2>UTF-7</h2>
<p><a title="UTF-7" href="http://zh.wikipedia.org/zh-cn/UTF-7">UTF-7</a> 是一个修改的Base64（<strong>Modified Base64</strong>）。主要是将<a title="UTF-16" href="http://zh.wikipedia.org/zh-cn/UTF-16">UTF-16</a>的数 据，用Base64的方法编码为可打印的 <a title="ASCII" href="http://zh.wikipedia.org/zh-cn/ASCII">ASCII</a> 字符序列。目的是传输 Unicode  数据。主要的区别在于不用等号&#8221;=&#8221;补余，因为该字符通常需要大量的转译。</p>
<h2>在URL中的应用</h2>
<p>Base64编码可用于在<a title="HTTP" href="http://zh.wikipedia.org/zh-cn/HTTP">HTTP</a>环境下传递较长的标识信息。例如，在<a title="Java" href="http://zh.wikipedia.org/zh-cn/Java">Java</a><a title="持久化（尚未撰写）" href="http://zh.wikipedia.org/w/index.php?title=%E6%8C%81%E4%B9%85%E5%8C%96&amp;action=edit&amp;redlink=1">持久化</a>系统<a title="Hibernate (Java)（尚未撰写）" href="http://zh.wikipedia.org/w/index.php?title=Hibernate_%28Java%29&amp;action=edit&amp;redlink=1">Hibernate</a>中，就采用了Base64来将一 个较长的唯一标识符（一般为128-bit的<a title="UUID" href="http://zh.wikipedia.org/zh-cn/UUID">UUID</a>）编码为一个字符串，用作HTTP表单和HTTP GET <a title="URL" href="http://zh.wikipedia.org/zh-cn/URL">URL</a>中 的参数。在其他应用程序中，也常常需要把二进制数据编码为适合放在URL（包括隐藏表单域）中的形式。此时，采用Base64编码不仅比较简短，同时也具 有不可读性，即所编码的数据不会被人用肉眼所直接看到。</p>
<p>然而，标准的Base64并不适合直接放在URL里传输，因为URL编码器会把标准Base64中的“/”和“+”字符变为形如“%XX”的形式， 而这些“%”号在存入数据库时还需要再进行转换，因为<a title="ANSI" href="http://zh.wikipedia.org/zh-cn/ANSI">ANSI</a> <a title="SQL" href="http://zh.wikipedia.org/zh-cn/SQL">SQL</a>中已将“%”号用作通配 符。</p>
<p>为解决此问题，可采用一种<strong>用于URL的改进Base64</strong>编码，它不在末尾填充&#8217;='号，并将标准Base64中的“+”和“/”分别 改成了“*”和“-”，这样就免去了在URL编解码和数据库存储时所要作的转换，避免了编码信息长度在此过程中的增加，并统一了数据库、表单等处对象标识 符的格式。</p>
<p>另有一种<strong>用于正则表达式的改进Base64</strong>变种，它将“+”和“/”改成了“!”和“-”，因为“+”，“*”以及前面在IRCu中 用到的“[”和“]”在<a title="正则表达式" href="http://zh.wikipedia.org/zh-cn/%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F">正则表达式</a>中都可能具有特殊含义。</p>
<p>此外还有一些变种，它们将“+/”改为“_-”或“._”（用作编程语言中的标识符名称）或“.-”（用于<a title="XML" href="http://zh.wikipedia.org/zh-cn/XML">XML</a>中的<em>Nmtoken</em>） 甚至“_:”（用于XML中的<em>Name</em>）。</p>
<p>关于 Base64可以参照网上另一篇文章：<a href="http://www.5dmail.net/html/2004-1-30/200413084348.htm" target="_blank">浅谈Base64编码</a></pre>
]]></content:encoded>
			<wfw:commentRss>http://www.javachen.com/2010/01/what-is-base64/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>What is MIME？</title>
		<link>http://www.javachen.com/2010/01/what-is-mime%ef%bc%9f/</link>
		<comments>http://www.javachen.com/2010/01/what-is-mime%ef%bc%9f/#comments</comments>
		<pubDate>Tue, 26 Jan 2010 09:44:32 +0000</pubDate>
		<dc:creator>JavaChen</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[OpenSource]]></category>
		<category><![CDATA[Base64]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[http]]></category>
		<category><![CDATA[MIME]]></category>

		<guid isPermaLink="false">http://www.javachen.com/?p=908</guid>
		<description><![CDATA[这是一篇普及互联网术语的文章。每天呆在互联网上，发现呆的时间越长，发现自己不知道的东西越多。互联网上有太多的知识，虽然我们不可能一下子了解透彻，但是遇到一个就有必要去了解其相关的知识或是原理，这样以后再见到他就不会觉得陌生了。 在维基百科上找到了关于MIME的介绍：http://zh.wikipedia.org/zh-cn/MIME，这里简要的介绍一下相关知识，便于以后查阅。 按照RFC2045的定义，Base64被定义为：Base64内容传送编码被设计用来把任意序列的8位字节描述为一种不易被人直接识别的形式。（The Base64 Content-Transfer-Encoding is designed to represent arbitrary sequences of octets in a form that need not be humanly readable.） 多用途互联网邮件扩展（MIME，Multipurpose Internet Mail Extensions）是一个互联网标准，它扩展了电子邮件标准，使其能够支持非ASCII字符、二进制格式附件等多种格式的邮件消息。这个标准被定义在RFC 2045、RFC 2046、RFC 2047、RFC 2048、RFC 2049等RFC中。 由RFC 822转变而来的RFC 2822，规定电子邮件标准并不允许在邮件消息中使用7位ASCII字符集以外的字符。正因如此，一些非英语字符消息和二进制文件，图像，声音等非文字消息都不能在电子邮件中传输。MIME规定了用于表示各种各样的数据类型的符号化方法。 此外，在万维网中使用的HTTP协议中也使用了MIME的框架。 IME是通过标准化电子邮件报文的头部的附加领域（fields）而实现的；这些头部的附加领域，描述新的报文类型的内容和组织形式。目前版本是1.0。 内容类型（Content-Type），这个头部领域用于指定消息的类型。一般以下面的形式出现。 Content-Type: [type]/[subtype]; parameter type有下面的形式。 * Text：用于标准化地表示的文本信息，文本消息可以是多种字符集和或者多种格式的； * Multipart：用于连接消息体的多个部分构成一个消息，这些部分可以是不同类型的数据； * Application：用于传输应用程序数据或者二进制数据； * Message：用于包装一个E-mail消息； * Image：用于传输静态图片数据； * Audio：用于传输音频或者音声数据； * Video：用于传输动态影像数据，可以是与音频编辑在一起的视频数据格式。 [...]]]></description>
			<content:encoded><![CDATA[<p>这是一篇普及互联网术语的文章。每天呆在互联网上，发现呆的时间越长，发现自己不知道的东西越多。互联网上有太多的知识，虽然我们不可能一下子了解透彻，但是遇到一个就有必要去了解其相关的知识或是原理，这样以后再见到他就不会觉得陌生了。</p>
<p>在维基百科上找到了关于MIME的介绍：<a href="http://zh.wikipedia.org/zh-cn/MIME" target="_blank">http://zh.wikipedia.org/zh-cn/MIME</a>，这里简要的介绍一下相关知识，便于以后查阅。<span id="more-908"></span></p>
<p>按照RFC2045的定义，Base64被定义为：Base64内容传送编码被设计用来把任意序列的8位字节描述为一种不易被人直接识别的形式。（The  Base64 Content-Transfer-Encoding is designed to represent arbitrary  sequences of octets in a form that need not be humanly readable.）</p>
<p>多用途互联网邮件扩展（MIME，Multipurpose Internet Mail Extensions）是一个互联网标准，它扩展了电子邮件标准，使其能够支持非ASCII字符、二进制格式附件等多种格式的邮件消息。这个标准被定义在RFC 2045、RFC 2046、RFC 2047、RFC 2048、RFC 2049等RFC中。 由RFC 822转变而来的RFC 2822，规定电子邮件标准并不允许在邮件消息中使用7位ASCII字符集以外的字符。正因如此，一些非英语字符消息和二进制文件，图像，声音等非文字消息都不能在电子邮件中传输。MIME规定了用于表示各种各样的数据类型的符号化方法。 此外，在万维网中使用的HTTP协议中也使用了MIME的框架。<br />
IME是通过标准化电子邮件报文的头部的附加领域（fields）而实现的；这些头部的附加领域，描述新的报文类型的内容和组织形式。目前版本是1.0。<br />
内容类型（Content-Type），这个头部领域用于指定消息的类型。一般以下面的形式出现。</p>
<p>Content-Type: [type]/[subtype]; parameter</p>
<p>type有下面的形式。</p>
<p>* Text：用于标准化地表示的文本信息，文本消息可以是多种字符集和或者多种格式的；<br />
* Multipart：用于连接消息体的多个部分构成一个消息，这些部分可以是不同类型的数据；<br />
* Application：用于传输应用程序数据或者二进制数据；<br />
* Message：用于包装一个E-mail消息；<br />
* Image：用于传输静态图片数据；<br />
* Audio：用于传输音频或者音声数据；<br />
* Video：用于传输动态影像数据，可以是与音频编辑在一起的视频数据格式。</p>
<p>subtype用于指定type的详细形式。content-type/subtype配对的集合和与此相关的参数，将随着时间而增长。为了确保这些值在一个有序而且公开的状态下开发，MIME使用Internet Assigned Numbers Authority (IANA)作为中心的注册机制来管理这些值。常用的subtype值如下所示：</p>
<p>* text/plain（纯文本）<br />
* text/html（HTML文档）<br />
* application/xhtml+xml（XHTML文档）<br />
* image/gif（GIF图像）<br />
* image/jpeg（JPEG图像）<br />
* image/png（PNG图像）<br />
* video/mpeg（MPEG动画）<br />
* application/octet-stream（任意的二进制数据）<br />
* application/pdf（PDF文档）<br />
* application/msword（Microsoft Word文件）<br />
* message/rfc822（RFC 822形式）<br />
* multipart/alternative（HTML邮件的HTML形式和纯文本形式，相同内容使用不同形式表示）<br />
* application/x-www-form-urlencoded（使用HTTP的POST方法提交的表单）<br />
* multipart/form-data（同上，但主要用于表单提交时伴随文件上传的场合）</p>
<p>此外，尚未被接受为正式数据类型的subtype，可以使用x-开始的独立名称（例如application/x-gzip）。vnd-开始的固有名称也可以使用（例：application/vnd.ms-excel）。</p>
<p>parameter可以用来指定附加的信息，更多情况下是用于指定text/plain和text/htm等的文字编码方式的charset参数。 MIME根据type制定了默认的subtype，当客户端不能确定消息的subtype的情况下，消息被看作默认的subtype进行处理。Text默认是text/plain，Application默认是application/octet-stream而Multipart默认情况下被看作 multipart/mixed。<br />
内容传输编码（Content-Transfer-Encoding），这个区域使指定ASCII以外的字符编码方式成为可能。形式如下：</p>
<p>Content-Transfer-Encoding: [mechanism]</p>
<p>其中，mechanism的值可以指定为“7bit”，“8bit”，“binary”，“quoted- printable”，“base64”。</p>
<p>详细的介绍，可以参阅：<a href="http://zh.wikipedia.org/zh-cn/MIME" target="_blank">http://zh.wikipedia.org/zh-cn/MIME</a></p>
<p>你还可以观看本网站内的同类文章：</p>
<ol>
<li> <a href="http://www.javachen.com/2010/01/what-is-kpi/" target="_blank">什么是KPI？</a></li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://www.javachen.com/2010/01/what-is-mime%ef%bc%9f/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
