请教关于MIME协议的问题 难道字符表不同也叫Base64????? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 sun.misc.BASE64Encoder enc = new sun.misc.BASE64Encoder(); msg.setSubject("=?GB2312?B?"+enc.encode(subject.getBytes())+"?=");一个进行Base64编码的类 --------------------------------------------------------------------------------源作者:sonymusic 人气:5543 package sony.utils; import java.io.*; import java.net.*; public final class Codes { public final static byte[] base64Encode(byte[] byteData) { if (byteData == null) return null; int iSrcIdx; // index into source (byteData) int iDestIdx; // index into destination (byteDest) byte byteDest[] = new byte[((byteData.length + 2) / 3) * 4]; for (iSrcIdx = 0, iDestIdx = 0; iSrcIdx < byteData.length - 2; iSrcIdx += 3) { byteDest[iDestIdx++] = (byte) ((byteData[iSrcIdx] >>> 2) & 077); byteDest[iDestIdx++] = (byte) ((byteData[iSrcIdx + 1] >>> 4) & 017 | (byteData[iSrcIdx] << 4) & 077); byteDest[iDestIdx++] = (byte) ((byteData[iSrcIdx + 2] >>> 6) & 003 | (byteData[iSrcIdx + 1] << 2) & 077); byteDest[iDestIdx++] = (byte) (byteData[iSrcIdx + 2] & 077); } if (iSrcIdx < byteData.length) { byteDest[iDestIdx++] = (byte) ((byteData[iSrcIdx] >>> 2) & 077); if (iSrcIdx < byteData.length - 1) { byteDest[iDestIdx++] = (byte) ((byteData[iSrcIdx + 1] >>> 4) & 017 | (byteData[iSrcIdx] << 4) & 077); byteDest[iDestIdx++] = (byte) ((byteData[iSrcIdx + 1] << 2) & 077); } else byteDest[iDestIdx++] = (byte) ((byteData[iSrcIdx] << 4) & 077); } for (iSrcIdx = 0; iSrcIdx < iDestIdx; iSrcIdx++) { if (byteDest[iSrcIdx] < 26) byteDest[iSrcIdx] = (byte) (byteDest[iSrcIdx] + ´A´); else if (byteDest[iSrcIdx] < 52) byteDest[iSrcIdx] = (byte) (byteDest[iSrcIdx] + ´a´ - 26); else if (byteDest[iSrcIdx] < 62) byteDest[iSrcIdx] = (byte) (byteDest[iSrcIdx] + ´0´ - 52); else if (byteDest[iSrcIdx] < 63) byteDest[iSrcIdx] = (byte) ´+´; else byteDest[iSrcIdx] = (byte) ´/´; } for (; iSrcIdx < byteDest.length; iSrcIdx++) byteDest[iSrcIdx] = (byte) ´=´; return byteDest; } public final static String base64Encode(String strInput) { if (strInput == null) return null; return base64Encode(strInput,"GB2312"); } public final static String base64Encode(String strInput,String charSet) { if (strInput == null) return null; String strOutput=null; byte byteData[] = new byte[strInput.length()]; try { //strInput.getBytes(0, strInput.length(), byteData, 0); byteData = strInput.getBytes(charSet); strOutput=new String(base64Encode(byteData),charSet); //strOutput=new String(base64Encode(byteData),0); } catch (UnsupportedEncodingException e) { return null; } return strOutput; } /** * 此处插入方法说明。 * 创建日期:(2000-11-4 18:27:35) * @param steam java.io.InputStream * @param charSet java.lang.String */ public final static String base64Encode(InputStream in, String charSet) { try { int c; byte[] buff = new byte[1024]; ByteArrayOutputStream out = new ByteArrayOutputStream(2048); while ((c = in.read(buff, 0, 1024)) != -1) { out.write(buff, 0, c); //index+=1024; //out.write(c); //attachContent+=ss; } in.close(); out.flush(); byte[] tmp2 = Codes.base64Encode(out.toByteArray()); out.close(); return new String(tmp2,charSet); } catch (IOException e) { return ""; } }/** * 此处插入方法说明。 * 创建日期:(2000-11-3 23:31:04) * @return java.lang.String * @param strIn java.lang.String */ public final static String chunkSplit(String strIn) { return chunkSplit(strIn,76); }/** * 此处插入方法说明。 * 创建日期:(2000-11-3 23:31:04) * @return java.lang.String * @param strIn java.lang.String */ public final static String chunkSplit(String strIn,int splitLen) { int index=0; String strOut=""; while(index+splitLen<strIn.length()){ strOut+=strIn.substring(index,index+splitLen)+"\n"; index+=splitLen; } if(index<strIn.length()){ strOut+=strIn.substring(index); } return strOut; } } to zealVampire(蚊子)两种方法都报错说没有enc.encode()方法下面这几句我看不懂,编译报错byteDest[iSrcIdx] = (byte) (byteDest[iSrcIdx] + ´A´); else if (byteDest[iSrcIdx] < 52) byteDest[iSrcIdx] = (byte) (byteDest[iSrcIdx] + ´a´ - 26); else if (byteDest[iSrcIdx] < 62) byteDest[iSrcIdx] = (byte) (byteDest[iSrcIdx] + ´0´ - 52); else if (byteDest[iSrcIdx] < 63) byteDest[iSrcIdx] = (byte) ´+´; else byteDest[iSrcIdx] = (byte) ´/´; Base64遇到中文时,具体如何处理呢? 用javax.mail.internet.MimeUtility,如:String subject = "=?ISO-8859-1?B?SWYgeW91IGNhbiByZWFkIHRoaXMgeW8=?=\r\n=?ISO-8859-1?B?dSB1bmRlcnN0YW5kIHRoZSBleGFtcGxlLg==?=";System.out.println(MimeUtility.decodeText(subject));System.out.println(MimeUtility.decodeText("=?GBK?B?1tDOxA==?=")); //中文 问题解决了不知为什么,中文编码结果最后都要-128我还是用我自己实现的Base64编码解码器吧谢谢各位的帮助结帖 import java.net.*;import java.io.*;import java.util.*; //enc.encode()方法 有的//我不用javaMail发邮件的时候就是用他将用户名和密码转换成Base64的 【求助】Java线程池入门问题【在线急等】 log4j配置问题?? 如何统一管理所有页面的控件 父类指针 高分求applet做的算法演示系统 JTree问题为什么显示不出来?谢谢! ---见了鬼了,Jre不好用!!!--- 建议:所有程序员打工者联合起来,推荐一些值得去的好公司,揭露一些去不得的公司! 编译成功APPLETVIEWER成功运行,可是在WEB页上却看不到是怎么回事? BufferedReader使用readLine()方法返回一个奇葩的null值,麻烦指教。 除了SUN官方网站,还有哪里有JDK14-DOC可下载? user.home是如何在得到的?(可能比较少见)
一个进行Base64编码的类 --------------------------------------------------------------------------------
源作者:sonymusic 人气:5543
package sony.utils; import java.io.*;
import java.net.*;
public final class Codes {
public final static byte[] base64Encode(byte[] byteData) {
if (byteData == null)
return null;
int iSrcIdx; // index into source (byteData)
int iDestIdx; // index into destination (byteDest)
byte byteDest[] = new byte[((byteData.length + 2) / 3) * 4]; for (iSrcIdx = 0, iDestIdx = 0; iSrcIdx < byteData.length - 2; iSrcIdx += 3) {
byteDest[iDestIdx++] = (byte) ((byteData[iSrcIdx] >>> 2) & 077);
byteDest[iDestIdx++] =
(byte) ((byteData[iSrcIdx + 1] >>> 4) & 017 | (byteData[iSrcIdx] << 4) & 077);
byteDest[iDestIdx++] =
(byte) ((byteData[iSrcIdx + 2] >>> 6)
& 003
| (byteData[iSrcIdx + 1] << 2)
& 077);
byteDest[iDestIdx++] = (byte) (byteData[iSrcIdx + 2] & 077);
} if (iSrcIdx < byteData.length) {
byteDest[iDestIdx++] = (byte) ((byteData[iSrcIdx] >>> 2) & 077);
if (iSrcIdx < byteData.length - 1) {
byteDest[iDestIdx++] =
(byte) ((byteData[iSrcIdx + 1] >>> 4) & 017 | (byteData[iSrcIdx] << 4) & 077);
byteDest[iDestIdx++] = (byte) ((byteData[iSrcIdx + 1] << 2) & 077);
} else
byteDest[iDestIdx++] = (byte) ((byteData[iSrcIdx] << 4) & 077);
} for (iSrcIdx = 0; iSrcIdx < iDestIdx; iSrcIdx++) {
if (byteDest[iSrcIdx] < 26)
byteDest[iSrcIdx] = (byte) (byteDest[iSrcIdx] + ´A´);
else
if (byteDest[iSrcIdx] < 52)
byteDest[iSrcIdx] = (byte) (byteDest[iSrcIdx] + ´a´ - 26);
else
if (byteDest[iSrcIdx] < 62)
byteDest[iSrcIdx] = (byte) (byteDest[iSrcIdx] + ´0´ - 52);
else
if (byteDest[iSrcIdx] < 63)
byteDest[iSrcIdx] = (byte) ´+´;
else
byteDest[iSrcIdx] = (byte) ´/´;
} for (; iSrcIdx < byteDest.length; iSrcIdx++)
byteDest[iSrcIdx] = (byte) ´=´; return byteDest;
}
public final static String base64Encode(String strInput) {
if (strInput == null)
return null;
return base64Encode(strInput,"GB2312");
}
public final static String base64Encode(String strInput,String charSet) {
if (strInput == null)
return null;
String strOutput=null;
byte byteData[] = new byte[strInput.length()];
try {
//strInput.getBytes(0, strInput.length(), byteData, 0);
byteData = strInput.getBytes(charSet);
strOutput=new String(base64Encode(byteData),charSet);
//strOutput=new String(base64Encode(byteData),0);
} catch (UnsupportedEncodingException e) {
return null;
}
return strOutput;
}
/**
* 此处插入方法说明。
* 创建日期:(2000-11-4 18:27:35)
* @param steam java.io.InputStream
* @param charSet java.lang.String
*/
public final static String base64Encode(InputStream in, String charSet) {
try {
int c;
byte[] buff = new byte[1024];
ByteArrayOutputStream out = new ByteArrayOutputStream(2048);
while ((c = in.read(buff, 0, 1024)) != -1) {
out.write(buff, 0, c);
//index+=1024;
//out.write(c);
//attachContent+=ss;
}
in.close();
out.flush();
byte[] tmp2 = Codes.base64Encode(out.toByteArray());
out.close();
return new String(tmp2,charSet);
}
catch (IOException e) {
return "";
}
}/**
* 此处插入方法说明。
* 创建日期:(2000-11-3 23:31:04)
* @return java.lang.String
* @param strIn java.lang.String
*/
public final static String chunkSplit(String strIn) {
return chunkSplit(strIn,76);
}/**
* 此处插入方法说明。
* 创建日期:(2000-11-3 23:31:04)
* @return java.lang.String
* @param strIn java.lang.String
*/
public final static String chunkSplit(String strIn,int splitLen) {
int index=0;
String strOut="";
while(index+splitLen<strIn.length()){
strOut+=strIn.substring(index,index+splitLen)+"\n";
index+=splitLen;
}
if(index<strIn.length()){
strOut+=strIn.substring(index);
}
return strOut;
}
}
说没有enc.encode()方法下面这几句我看不懂,编译报错
byteDest[iSrcIdx] = (byte) (byteDest[iSrcIdx] + ´A´);
else
if (byteDest[iSrcIdx] < 52)
byteDest[iSrcIdx] = (byte) (byteDest[iSrcIdx] + ´a´ - 26);
else
if (byteDest[iSrcIdx] < 62)
byteDest[iSrcIdx] = (byte) (byteDest[iSrcIdx] + ´0´ - 52);
else
if (byteDest[iSrcIdx] < 63)
byteDest[iSrcIdx] = (byte) ´+´;
else
byteDest[iSrcIdx] = (byte) ´/´;
String subject = "=?ISO-8859-1?B?SWYgeW91IGNhbiByZWFkIHRoaXMgeW8=?=\r\n=?ISO-8859-1?B?dSB1bmRlcnN0YW5kIHRoZSBleGFtcGxlLg==?=";
System.out.println(MimeUtility.decodeText(subject));System.out.println(MimeUtility.decodeText("=?GBK?B?1tDOxA==?=")); //中文
不知为什么,中文编码结果最后都要-128我还是用我自己实现的Base64编码解码器吧谢谢各位的帮助结帖
import java.io.*;
import java.util.*;
//enc.encode()方法 有的
//我不用javaMail发邮件的时候就是用他将用户名和密码转换成Base64的