import java.io.IOException; import java.io.UnsupportedEncodingException; import java.net.URLDecoder; import java.net.URLEncoder; import org.apache.log4j.Logger; import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; public class URLDoBase64 { private static final Logger LOG = Logger.getLogger(URLDoBase64.class); /** * 对base64编码的字符进行编码处理,以便于http传播 处理规则 ? (AAA) & (BBB) = (CCC) / (DDD) , (EEE) + (FFF) % (GGG) < * (HHH) > (III) # (JJJ) * * @param str * @return */ public static String enURL(String str) { String sbase64=null; try { sbase64 = Base64.encode(str.getBytes("UTF-8")); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return sbase64.replaceAll("\?", "\(AAA\)").replaceAll("&", "\(BBB\)").replaceAll("=", "\(CCC\)").replaceAll("\/", "\(DDD\)").replaceAll(",", "\(EEE\)").replaceAll("\+", "\(FFF\)").replaceAll("%", "\(GGG\)").replaceAll("<", "\(HHH\)").replaceAll(">", "\(III\)").replaceAll("#", "\(JJJ\)").replaceAll(" ", "\(KKK\)"); } /** * 对base64编码的字符进行解码处理 处理规则 ? (AAA) & (BBB) = (CCC) / (DDD) , (EEE) + (FFF) % (GGG) < (HHH) > * (III) # (JJJ) * * @param str * @return */ public static String deURL(String str) { String re=str.replaceAll("\(AAA\)", "?").replaceAll("\(BBB\)", "&").replaceAll("\(CCC\)", "=").replaceAll("\(DDD\)", "/").replaceAll("\(EEE\)", ",").replaceAll("\(FFF\)", "+").replaceAll("\(GGG\)", "%").replaceAll("\(HHH\)", "<").replaceAll("\(III\)", ">").replaceAll("\(JJJ\)", "#").replaceAll("\(KKK\)", " "); byte [] bBase64 = Base64.decode(re); String s=null; try { s = new String(bBase64,"utf-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return s; } public static void main(String[] args) { byte[] data={(byte) 0xfb,(byte)0xf0,(byte)0x01};
String encodeBase64 = new BASE64Encoder().encode(data); String safeBase64Str = encodeBase64.replace('+', '-'); safeBase64Str = safeBase64Str.replace('/', '_'); safeBase64Str = safeBase64Str.replaceAll("=", "");
String s ="55(FFF)t5L(FFF)h57yW56CB5LiN6IO95Li656m6"; System.out.println(deURL(s));
} } |