您的位置:首页 > 编程语言 > Java开发

java的quoted-printable解码器源代码

2015-08-30 14:32 501 查看
转自 http://www.qqread.com/java/c572396600.html
package test;

import java.io.ByteArrayOutputStream;

public class QuotedPrintable {

public static void main(String[] args) {
String a = "=BC=DA=A5=DF=A7Q=B3=A1=B8=A8=AE=E6=A1G[=AEi=C4=FD=B3]=ADp]HELLO INDUSTRY 4.0=A1A=A4u=B7~4.0=AE=C9=A5N=A5=BF=A6=A1=B1=D2=B0=CA=A1COLILY x OYA presents";
String b = qpDecoding(a);
System.out.println(b);
}

/**
* QuotedPrintable 解码
* @param str
* @return
*/
public final static String qpDecoding(String str) {
if (str == null) {
return "";
}
try {
str = str.replaceAll("=\n", "");
byte[] bytes = str.getBytes("US-ASCII");
for (int i = 0; i < bytes.length; i++) {
byte b = bytes[i];
if (b != 95) {
bytes[i] = b;
} else {
bytes[i] = 32;
}
}
if (bytes == null) {
return "";
}
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
for (int i = 0; i < bytes.length; i++) {
int b = bytes[i];
if (b == '=') {
try {
int u = Character.digit((char) bytes[++i], 16);
int l = Character.digit((char) bytes[++i], 16);
if (u == -1 || l == -1) {
continue;
}
buffer.write((char) ((u << 4) + l));
} catch (ArrayIndexOutOfBoundsException e) {
e.printStackTrace();
}
} else {
buffer.write(b);
}
}
return new String(buffer.toByteArray(), "big5");// big5
} catch (Exception e) {
e.printStackTrace();
return "";
}
}
}
输出结果: 歐立利部落格:[展覽設計]HELLOINDUSTRY4.0,工業4.0時代正式啟動。OLILYxOYApresents
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: