地址栏加密解密

[quote]
[img]http://anzhoujava.iteye.com/upload/picture/pic/67077/729b559b-0f76-36b6-86b6-60dd30900d32.jpg[/img]

package com.java.unit;

import java.io.ByteArrayOutputStream;

public class StringUtil {
private static final String VERIABLY =
"abcdefghijklmnopqrstuvwxyz"+
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"+
".-*_";
public static String urlEncode(
String s, String charset) throws Exception {
// abc
byte[] bytes =
s.getBytes(charset);
StringBuilder sb = new StringBuilder();
outer:
for(int i=0;i<bytes.length;i++) {
byte b = bytes[i];
if(b == ' ') {
sb.append('+');
continue outer;
}
for(int j=0;j<VERIABLY.length();j++) {
if(VERIABLY.charAt(j) == b) {
sb.append((char) b);
continue outer;
}
}
String hex = Integer.toHexString(b&0x000000ff);
if(hex.length() == 1) hex = '0'+hex;
hex = '%'+hex;
sb.append(hex);
}
return sb.toString();
}

public static String urlDecode(String s,String charset) throws Exception {
ByteArrayOutputStream out =
new ByteArrayOutputStream();

for(int i=0;i<s.length();i++) {
char c = s.charAt(i);
if(c == '%') {
// %f8
String hex = "";
hex += s.charAt(++i);
hex += s.charAt(++i);
int n = Integer.parseInt(hex, 16);
out.write(n);
} else {
if(VERIABLY.indexOf(c) != -1) {
out.write(c);
continue;
}
if('+' == c) {
out.write(' ');
continue;
}
}
}
byte[] data = out.toByteArray();
return new String(data,charset);
}

public static void main(String[] args) throws Exception {
String s = urlEncode("游戏","UTF-8");
System.out.println(s);

s = urlDecode("%e6%b8%b8%e6%88%8f", "UTF-8");
System.out.println(s);
}
}


[/quote]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值