ITeye博客

BASE64编码解码

dbscx · 2010-12-08 17:27 · JAVA · 编程语言 · Markdown

webservices中需要将声明为byte[]的类型进行base64编码传输

在传输大量业务数据的String和图片时可以采用,而且避免明文传输带来的风险

 

public static String encodeBASE64(String s) {
	if (s == null)
		return null;
	return (new sun.misc.BASE64Encoder()).encode(s.getBytes());
}

public static String decodeBASE64(String s) {
	if (s == null)
		return null;
	sun.misc.BASE64Decoder decoder = new sun.misc.BASE64Decoder();
	try {
		byte[] b = decoder.decodeBuffer(s);
		return new String(b);
	} catch (Exception e) {
		return null;
	}
}

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。