{"id":5811,"date":"2024-08-31T10:01:04","date_gmt":"2024-08-31T02:01:04","guid":{"rendered":""},"modified":"2024-08-31T10:01:04","modified_gmt":"2024-08-31T02:01:04","slug":"\u5b57\u7b26\u4e32\u8f6c\u7801_\u4e07\u80fd\u7f16\u7801\u8f6c\u6362\u5668","status":"publish","type":"post","link":"https:\/\/mushiming.com\/5811.html","title":{"rendered":"\u5b57\u7b26\u4e32\u8f6c\u7801_\u4e07\u80fd\u7f16\u7801\u8f6c\u6362\u5668"},"content":{"rendered":"
package com.searchpic.framework.util;<\/p>\n
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.net.URLDecoder;
\/**
* <p>Title:\u5b57\u7b26\u7f16\u7801\u5de5\u5177\u7c7b <\/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 {
\n
\/**
* \u8f6c\u6362\u7f16\u7801 ISO-8859-1\u5230GB2312
* @param text
* @return
*\/
public String ISO2GB(String text) {
\n
String result = \"\";
try {
\n
result = new String(text.getBytes(\"ISO-8859-1\"), \"GB2312\");
}
catch (UnsupportedEncodingException ex) {
\n
result = ex.toString();
}
return result;
}
\/**
* \u8f6c\u6362\u7f16\u7801 GB2312\u5230ISO-8859-1
* @param text
* @return
*\/
public String GB2ISO(String text) {
\n
String result = \"\";
try {
\n
result = new String(text.getBytes(\"GB2312\"), \"ISO-8859-1\");
}
catch (UnsupportedEncodingException ex) {
\n
ex.printStackTrace();
}
return result;
}
\/**
* Utf8URL\u7f16\u7801
* @param s
* @return
*\/
public String Utf8URLencode(String text) {
\n
StringBuffer result = new StringBuffer();
for (int i = 0; i < text.length(); i++) {
\n
char c = text.charAt(i);
if (c >= 0 && c <= 255) {
\n
result.append(c);
}else {
\n
byte[] b = new byte[0];
try {
\n
b = Character.toString(c).getBytes(\"UTF-8\");
}catch (Exception ex) {
\n
}
for (int j = 0; j < b.length; j++) {
\n
int k = b[j];
if (k < 0) k += 256;
result.append(\"%\" + Integer.toHexString(k).toUpperCase());
}
}
}
return result.toString();
}
\/**
* Utf8URL\u89e3\u7801
* @param text
* @return
*\/
public String Utf8URLdecode(String text) {
\n
String result = \"\";
int p = 0;
if (text!=null && text.length()>0){
\n
text = text.toLowerCase();
p = text.indexOf(\"%e\");
if (p == -1) return text;
while (p != -1) {
\n
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\u7f16\u7801\u8f6c\u5b57\u7b26
* @param text
* @return
*\/
private String CodeToWord(String text) {
\n
String result;
if (Utf8codeCheck(text)) {
\n
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 {
\n
result = new String(code, \"UTF-8\");
}catch (UnsupportedEncodingException ex) {
\n
result = null;
}
}
else {
\n
result = text;
}
return result;
}
\/**
* \u7f16\u7801\u662f\u5426\u6709\u6548
* @param text
* @return
*\/
private boolean Utf8codeCheck(String text){
\n
String sign = \"\";
if (text.startsWith(\"%e\"))
for (int i = 0, p = 0; p != -1; i++) {
\n
p = text.indexOf(\"%\", p);
if (p != -1)
p++;
sign += p;
}
return sign.equals(\"147-1\");
}
\/**
* \u662f\u5426Utf8Url\u7f16\u7801
* @param text
* @return
*\/
public boolean isUtf8Url(String text) {
\n
text = text.toLowerCase();
int p = text.indexOf(\"%\");
if (p != -1 && text.length() - p > 9) {
\n
text = text.substring(p, p + 9);
}
return Utf8codeCheck(text);
}
\/**
* \u6d4b\u8bd5
* @param args
*\/<\/p>\n
public static void main(String[] args) {
\n
CharTools charTools = new CharTools();
String text=\"http:\/\/www.baidu.com\/baidu?word=\u4e2d\u56fd\u5927\u767e\u79d1\u5728\u7ebf\u5168\u6587\u68c0\u7d22&tn=myie2dg\";
\/\/ String a=URLDecoder.decode(text);
\/\/ String b=charTools.Utf8URLencode(\"\u5927\u54e5\");
String c=charTools.Utf8URLdecode(\"%E4%B8%AD%E5%9B%BD%E5%B7%A1%E5%9B%9E%E6%BC%94%E5%94%B1%EF%BF%BD%3F%E4%B8%8A%E6%B5%B7\");
\/\/ System.out.println(a);
\/\/ System.out.println(b);
System.out.println(c);
\/\/ 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)){
\n
\/\/ System.out.println(charTools.Utf8URLdecode(url));
\/\/ }else{
\n
\/\/ System.out.println(URLDecoder.decode(url));
\/\/ }
\/\/ 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)){
\n
\/\/ System.out.println(charTools.Utf8URLdecode(url));
\/\/ }else{
\n
\/\/ System.out.println(URLDecoder.decode(url));
\/\/ }
}
}
<\/p>\n","protected":false},"excerpt":{"rendered":"\u5b57\u7b26\u4e32\u8f6c\u7801_\u4e07\u80fd\u7f16\u7801\u8f6c\u6362\u5668packagecom.searchpic.framework.util;importjava.io.UnsupportedEncodingException;importjava.ne...","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"_links":{"self":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/5811"}],"collection":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/comments?post=5811"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/5811\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=5811"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=5811"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=5811"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}