---
title: "BASE64编码解码"
author: "dbscx"
date: "2010-12-08T17:27:51Z"
tags: []
categories: ["编程语言"]
slug: "base64"
draft: false
lastmod: "2010-12-08T17:27:51Z"
id: 836892
url: "https://dbscx.iteye.com/blog/836892"
---

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;
	}
}
```