Java tutorial
/** * Base64Util.java * * Copyright 2011 Baidu, Inc. * * Baidu licenses this file to you under the Apache License, version 2.0 * (the "License"); you may not use this file except in compliance with the * License. You may obtain a copy of the License at: * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations * under the License. */ package com.baidu.terminator.plugin.signer.customized.app; import java.io.UnsupportedEncodingException; import org.apache.commons.codec.binary.Base64; /** * Base64Util?byte64? * Base64Util?Base64?apachecommons * ?commons-codec-1.3.jar * * ??? * 1. public static final String byteToBase64(byte[] bytes) * byte??64 * * 2. public static final byte[] base64toByte(String str) * 64??byte * * @see org.apache.commons.codec.binary.Base64 * @used RSAUtil * @category veyron code -> -> ? * @author <a href="mailto:xiejiyun@baidu.com">Xie, Jiyun</a> * @version 1.0.0 */ public class Base64ForServer { /** * byte??64 * Encode Byte Array into Base64 String * * @param data ?byte * @return ??64 * @throws UnsupportedEncodingException ???UTF-8? */ public static String byteToBase64(byte[] data) throws UnsupportedEncodingException { if (data == null) throw new IllegalArgumentException("The parameter should not be null!"); return new String(Base64.encodeBase64(data), "UTF-8"); } /** * 64??byte * Decode Base64 String into Byte Array * * @param str ?64 * @return ??byte * @throws UnsupportedEncodingException ???UTF-8? */ public static byte[] base64toByte(String str) throws UnsupportedEncodingException { if (str == null) throw new IllegalArgumentException("The parameter should not be null!"); return Base64.decodeBase64(str.getBytes("UTF-8")); } }