java 各种编码小叙

时间:2021-09-30 15:59:13

                                                            内容转换成base64格式

转载于 http://www.javalaw.cn/forum/read.php?tid=331&page=e&fpage=2 

1、内容转换成base64格式
String ret=new String();
byte[] bytes=new byte[1024];
String aa="aaaa";
bytes=aa.getBytes();
ret=new sun.misc.BASE64Encoder().encode(bytes); //具体的编码方法
bytes = new sun.misc.BASE64Decoder().decodeBuffer(ret);  
aa=new String(bytes);
out.println(aa);

2、把文件转换成byte[]

private byte[] getContent(String fullfilename) throws IOException {
  FileInputStream f = new FileInputStream(fullfilename);
  byte[] b = new byte[ (int)new File(fullfilename).length()];
  f.read(b);
  f.close();
  return b;

3、在使用javamail开发发送邮件程序时,我对邮件html正文作了base64编码,请问,我如何指定MimeMessage的Content-Transfer-Encoding是base64,而不是bit7或者quoted-printable。
MimeBodyPart htmlodyPart = new MimeBodyPart();
    String content = "<html><body>" + "汉字"+ "</body><html>";
    htmlodyPart.setContent(content, "text/html;charset=GBK");
htmlodyPart.setHeader("Content-Transfer-Encoding", "base64");

4、示例

import java.io.*;

public class MIMEBase64 {
/*
这是个简单的Base64编码程序
作者:Roc Chen [email]rocanny@163.com[/email]
Base64 使用US-ASCII子集的65个字符, 每个字符用6位表示
因此"m"的Base64值为38, 二进制形式是 100110.

对于文本串,编码过程如下。例如"men":

先转成US-ASCII值.

"m"十进制 109
"e"十进制 101
"n"十进制 110

二进制 :

m 01101101
e 01100101
n 01101110

三个8位连起来是24位
011011010110010101101110

然后分成4个6位
011011 010110 010101 101110

现在得到4个值,十进制为
27 22 21 46

对应的 Base64 字符是 :
b W V u

编码总是基于3个字符,从而产生4个Base64字符。
如果只剩1或2个字符,使用特殊字符"="补齐Base64的4字。
如,编码"me"

01101101 01100101
0110110101100101
011011 010110 0101
111111 (与,补足6位)
011011 010110 010100
b W U
b W U = ("=" 补足4字符)

于是 "bWU=" 就是"me"的Base64值.

再如编码 "m"

01101101
011011 01
111111
011011 010000
b Q = =
于是 "bQ==" 就是"m"的Base64值.

值得注意的是,MIME规定一行最多76个字符.

*/

static String BaseTable[] = {
"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P",
"Q","R","S","T","U","V","W","X","Y","Z","a","b","c","d","e","f",
"g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v",
"w","x","y","z","0","1","2","3","4","5","6","7","8","9","+","/"
};


public static void encode(String filename, BufferedWriter out) {
try {
File f = new File(filename);
FileInputStream fin = new FileInputStream(filename);

// 读文件到BYTE数组
byte bytes[] = new byte[(int)(f.length())];
int n = fin.read(bytes);

if (n < 1) return; // 没有内容

byte buf[] = new byte[4]; // base64 字符数组

int n3byt = n / 3; // 3 bytes 组数
int nrest = n % 3; // 分组后剩余 bytes
int k = n3byt * 3; //
int linelength = 0; // 行长
int i = 0; // 指针

// 3-bytes 分组 ...
while ( i < k ) {
buf[0] = (byte)(( bytes[i] & 0xFC) >> 2);
buf[1] = (byte)(((bytes[i] & 0x03) << 4) |
((bytes[i+1] & 0xF0) >> 4));
buf[2] = (byte)(((bytes[i+1] & 0x0F) << 2) |
((bytes[i+2] & 0xC0) >> 6));
buf[3] = (byte)( bytes[i+2] & 0x3F);
send(out, BaseTable[buf[0]]);
send(out, BaseTable[buf[1]]);
send(out, BaseTable[buf[2]]);
send(out, BaseTable[buf[3]]);
/*
以上代码可以优化,但会难以理解
buf[0]= (byte)(b[i] >> 2);
buf[1]= (byte)(((b[i] & 0x03) << 4)|(b[i+1]>> 4));
buf[2]= (byte)(((b[i+1] & 0x0F)<< 2)|(b[i+2]>> 6));
buf[3]= (byte)(b[i+2] & 0x3F);
send(out,BaseTable[buf[0]]+BaseTable[buf[1]]+
BaseTable[buf[2]]+BaseTable[buf[3]]);
*/

if ((linelength += 4) >= 76) {
send(out, "/r/n");
linelength = 0;
}
i += 3;
}

// 处理尾部 ...
if (nrest==2) {
// 2 bytes left
buf[0] = (byte)(( bytes[k] & 0xFC) >> 2);
buf[1] = (byte)(((bytes[k] & 0x03) << 4) |
((bytes[k+1] & 0xF0) >> 4));
buf[2] = (byte)(( bytes[k+1] & 0x0F) << 2);
}
else if (nrest==1) {
// 1 byte left
buf[0] = (byte)((bytes[k] & 0xFC) >> 2);
buf[1] = (byte)((bytes[k] & 0x03) << 4);
}
if (nrest > 0) {
// 发送尾部
if ((linelength += 4) >= 76) send(out, "/r/n");
send(out, BaseTable[buf[0]]);
send(out, BaseTable[buf[1]]);
//
if (nrest==2) {
send(out, BaseTable[buf[2]]);
}
else {
send(out, "=");
}
send(out, "=");
}
out.flush();
//这里用到的send方法,请大家根据需要,自己写。可以是把结果输出到控制台,或发送邮件。
}
catch (Exception e) {
e.printStackTrace();
}
}
}