前段时间做URL的中文转换,有些url是utf8的格式,有的是gb2312的格式,很难区分到底是utf8还是gb2312,找了好久,发现网上的一个牛人写的转换代码:

package org.apache.hadoop.examples;
import java.io.UnsupportedEncodingException;
//import java.net.URLEncoder;
import java.net.URLDecoder;
/**
* <p>Title:字符编码工具类 </p>
* <p>Description: </p>
* <p>Copyright: flashman.com.cn Copyright (c) 2005</p>
* <p>Company: flashman.com.cn </p>
* @author: jeffzhu
* @version 1.0
*/
public class CharTools {
/**
* 转换编码 ISO-8859-1到GB2312
* @param text
* @return
*/
public String ISO2GB(String text) {
String result = "";
try {
result = new String(text.getBytes("ISO-8859-1"), "GB2312");
}
catch (UnsupportedEncodingException ex) {
result = ex.toString();
}
return result;
}
/**
* 转换编码 GB2312到ISO-8859-1
* @param text
* @return
*/
public String GB2ISO(String text) {
String result = "";
try {
result = new String(text.getBytes("GB2312"), "ISO-8859-1");
}
catch (UnsupportedEncodingException ex) {
ex.printStackTrace();
}
return result;
}
/**
* Utf8URL编码
* @param s
* @return
*/
public String Utf8URLencode(String text) {
StringBuffer result = new StringBuffer();
for (int i = 0; i < text.length(); i++) {
char c = text.charAt(i);
if (c >= 0 && c <= 255) {
result.append(c);
}else {
byte[] b = new byte[0];
try {
b = Character.toString(c).getBytes("UTF-8");
}catch (Exception ex) {
}
for (int j = 0; j < b.length; j++) {
int k = b[j];
if (k < 0) k += 256;
result.append("%" + Integer.toHexString(k).toUpperCase());
}
}
}
return result.toString();
}
/**
* Utf8URL解码
* @param text
* @return
*/
public String Utf8URLdecode(String text) {
String result = "";
int p = 0;
if (text!=null && text.length()>0){
text = text.toLowerCase();
p = text.indexOf("%e");
if (p == -1) return text;
while (p != -1) {
result += text.substring(0, p);
text = text.substring(p, text.length());
if (text == "" || text.length() < 9) return result;
result += CodeToWord(text.substring(0, 9));
text = text.substring(9, text.length());
p = text.indexOf("%e");
}
}
return result + text;
}
/**
* utf8URL编码转字符
* @param text
* @return
*/
private String CodeToWord(String text) {
String result;
if (Utf8codeCheck(text)) {
byte[] code = new byte[3];
code[0] = (byte) (Integer.parseInt(text.substring(1, 3), 16) - 256);
code[1] = (byte) (Integer.parseInt(text.substring(4, 6), 16) - 256);
code[2] = (byte) (Integer.parseInt(text.substring(7, 9), 16) - 256);
try {
result = new String(code, "UTF-8");
}catch (UnsupportedEncodingException ex) {
result = null;
}
}
else {
result = text;
}
return result;
}
/**
* 编码是否有效
* @param text
* @return
*/
private boolean Utf8codeCheck(String text){
String sign = "";
if (text.startsWith("%e"))
for (int i = 0, p = 0; p != -1; i++) {
p = text.indexOf("%", p);
if (p != -1)
p++;
sign += p;
}
return sign.equals("147-1");
}
/**
* 是否Utf8Url编码
* @param text
* @return
*/
public boolean isUtf8Url(String text) {
text = text.toLowerCase();
int p = text.indexOf("%");
if (p != -1 && text.length() - p > 9) {
text = text.substring(p, p + 9);
}
return Utf8codeCheck(text);
}
/**
* 测试
* @param args
*/
// public static void main(String[] args) throws Exception{
// CharTools charTools = new CharTools();
// String url;
// url = "http://www.google.com/search?hl=zh-CN&newwindow=1&q=%E4%B8%AD%E5%9B%BD%E5%A4%A7%E7%99%BE%E7%A7%91%E5%9C%A8%E7%BA%BF%E5%85%A8%E6%96%87%E6%A3%80%E7%B4%A2&btnG=%E6%90%9C%E7%B4%A2&lr=";
// if(charTools.isUtf8Url(url)){
// System.out.println(charTools.Utf8URLdecode(url));
// }else{
// System.out.println(URLDecoder.decode(url,"gb2312"));
// }
// url = "http://www.baidu.com/baidu?word=%D6%D0%B9%FA%B4%F3%B0%D9%BF%C6%D4%DA%CF%DF%C8%AB%CE%C4%BC%EC%CB%F7&tn=myie2dg";
// if(charTools.isUtf8Url(url)){
// System.out.println(charTools.Utf8URLdecode(url));
// }else{
// System.out.println(URLDecoder.decode(url,"gb2312"));
// }
// }
}

转:http://www.360doc.com/content/06/0829/16/6246_193641.shtml

JAVA对URL的解码【转】的更多相关文章

  1. java web url编码解码问题(下载中文名文件)

    问题描述:需要url直接访问中文名的文件,类似于在地址栏里直接输入http://localhost:8080/example/丽江旅游攻略.doc 来进行文件下载,tomcat的server.xml文 ...

  2. java中URL 的编码和解码函数

    java中URL 的编码和解码函数java.net.URLEncoder.encode(String s)和java.net.URLDecoder.decode(String s);在javascri ...

  3. java中文乱码解决之道(五)-----java是如何编码解码的

    在上篇博客中LZ阐述了java各个渠道转码的过程,阐述了java在运行过程中那些步骤在进行转码,在这些转码过程中如果一处出现问题就很有可能会产生乱码!下面LZ就讲述java在转码过程中是如何来进行编码 ...

  4. JS 和 Java 中URL特殊字符编码方式

    前几天遇到url特殊字符编码的问题,在这里整理一下: JavaScript 1.  编码 escape(String) 其中某些字符被替换成了十六进制的转义序列. 解码 unescape(String ...

  5. java 页面url传值中文乱码的解决方法

    parent.window.location.href 和 iframe中src的乱码问题.要在这两个url地址中传中文,必须加编码,然后再解码.编码:encodeURI(encodeURI(&quo ...

  6. Java中url传递中文参数取值乱码的解决方法

    java中URL参数中有中文值,传到服务端,在用request.getParameter()方法,得到的常常会是乱码,这将涉及到字符解码操作. 方法一: http://xxx.do?ptname=’我 ...

  7. java中文乱码解决之道(五)—–java是如何编码解码的

    原文出处:http://cmsblogs.com/?p=1491 在上篇博客中LZ阐述了java各个渠道转码的过程,阐述了java在运行过程中那些步骤在进行转码,在这些转码过程中如果一处出现问题就很有 ...

  8. cookie的中文乱码问题【URL编码解码】

    先搞明白为什么会乱码,为什么要转码: 在tomcat 8 之前,cookie中不能直接存储中文数据.需要将中文数据转码,一般采用URL编码(%E3).在tomcat 8 之后,cookie支持中文数据 ...

  9. java处理url中的特殊字符%等

    java处理url中的特殊字符(如&,%...) URL(Uniform Resoure Locator,统一资源定位器)是Internet中对资源进行统一定位和管理的标志.一个完整的URL包 ...

随机推荐

  1. 信噪比——信号加噪相关的知识

    信噪比:即Signal noise ratio , 即SNR: 它的单位为 dB, 公式为: SNR = 10lg(PS / PN), 其中 ps 表示信号的有效功率, pn 表示噪声的有效功率: 如 ...

  2. 阿里技术嘉年华-aDev内容感悟

    之前参见了ADC然后要求在组内做了个简单的分享,因为写这个PPT的时候ADC的资料还没分享,所以仅凭自己记忆写的一点感悟罢了. PPT下载

  3. 如何允许你的应用移动到SD卡?(转至http://blog.csdn.net/feng88724/article/details/6946670)

    我们在使用Android手机时发现,有的程序允许被移动到SD卡,而有的不行?这是为什么呢? 因为在Android 2.2版之后, Android应用才被允许移动到SD卡中.而在此之前开发的应用,全部没 ...

  4. C#.NET MVC 枚举转dictionary自动装载生成下拉框

      /// <summary> /// 枚举转SelectListItem /// </summary> public class Enum_Helper { /// < ...

  5. Spring中初始化bean和销毁bean的时候执行某个方法的详解

    关于在spring  容器初始化 bean 和销毁前所做的操作定义方式有三种: 第一种:通过注解@PostConstruct  和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作 ...

  6. 使用nginx反向代理解决前端跨域问题

    1. 首先去Nginx官网下载一个最新版本的Nginx,下载地址:http://nginx.org/en/download.html.我这里下载的版本是:nginx/Windows-1.12.0.下载 ...

  7. 配置Server.xml

    Service下面的Connector, Engine, Executor. 组件的目录结构,配置文件,配置节点.

  8. 创建 React-Native 工程时,如何指定特定的 React-Native 版本

    react-native 可能会出现一种情况,就是版本最高的可能出现有些东西不太稳定,这时候要用到旧的版本怎么办?就可以用以下方法创建项目. 0. 原因 创建新的 React-Native (以下简称 ...

  9. 长姿势 教你在qq空间上显示iPhone6尾巴

    下午刚午休完的时候,广州很多童鞋都感受到了震感,半青也感受到了,不仅如此,我还感受到了更大震感,那就是翻一下QQ空间动态,竟然看到有一位好友的尾巴竟然显示为“iPhone6”,顿时觉得该好友逼格太高了 ...

  10. EasyUI的功能树之异步树

    最近几个项目都用到了EasyUI这个Jquery框架,目前感觉起来还是很好使的,展示效果很好,帮助文档什么的资料很多,而且互联网上Easy粉很多,大多数拥护和喜爱EasyUI的粉丝们都愿意在网络平台互 ...