---
title: " BASE64 编码和解码"
author: "semicolon"
date: "2010-10-13T09:23:34Z"
tags: []
categories: ["编程语言"]
slug: "base64"
draft: false
lastmod: "2010-10-13T09:23:34Z"
id: 783311
url: "https://codingbug.iteye.com/blog/783311"
---

本文参考转发于： <http://www.zhuoda.org/elite/34479.html>


```java

public static String base64DecoderTool(final String str) throws SystemException{

		BASE64Decoder decoder = new BASE64Decoder();
		String returnStr = null;
		byte[] b;
		
		try {
			b = decoder.decodeBuffer(str);
			returnStr = new String(b , "gbk");
		} catch (Exception e) {
			System.out.println(e.getMessage());
			throw new SystemException("BASE64被解析的字符串不能是空！");
		} 		
		return returnStr;
	}


public static String formBASE64Tool(final String str) throws SystemException{
		if (str == null || str.trim().length() <= 0 ){
			throw new SystemException("BASE64所进行编码的字符串不能为空");
		}
			
		return (new sun.misc.BASE64Encoder()).encode( str.getBytes() ); 
	}

```