java 中类似js encodeURIComponent 函数的实现案例
我就废话不多说了,大家还是直接看代码吧~
importjava.io.UnsupportedEncodingException;
importjava.net.URLDecoder;
importjava.net.URLEncoder;
/**
*UtilityclassforJavaScriptcompatibleUTF-8encodinganddecoding.
*
*@seehttp://stackoverflow.com/questions/607176/java-equivalent-to-javascripts-encodeuricomponent-that-produces-identical-output
*@authorJohnTopley
*/
publicclassEncodingUtil{
/**
*DecodesthepassedUTF-8Stringusinganalgorithmthat'scompatiblewith
*JavaScript'sdecodeURIComponentfunction.Returns
*nulliftheStringisnull.
*
*@paramsTheUTF-8encodedStringtobedecoded
*@returnthedecodedString
*/
publicstaticStringdecodeURIComponent(Strings){
if(s==null){
returnnull;
}
Stringresult=null;
try{
result=URLDecoder.decode(s,"UTF-8");
}
//Thisexceptionshouldneveroccur.
catch(UnsupportedEncodingExceptione){
result=s;
}
returnresult;
}
/**
*EncodesthepassedStringasUTF-8usinganalgorithmthat'scompatible
*withJavaScript'sencodeURIComponentfunction.Returns
*nulliftheStringisnull.
*
*@paramsTheStringtobeencoded
*@returntheencodedString
*/
publicstaticStringencodeURIComponent(Strings){
Stringresult=null;
try{
result=URLEncoder.encode(s,"UTF-8")
.replaceAll("\\+","%20")
.replaceAll("\\%21","!")
.replaceAll("\\%27","'")
.replaceAll("\\%28","(")
.replaceAll("\\%29",")")
.replaceAll("\\%7E","~");
}
//Thisexceptionshouldneveroccur.
catch(UnsupportedEncodingExceptione){
result=s;
}
returnresult;
}
/**
*Privateconstructortopreventthisclassfrombeinginstantiated.
*/
privateEncodingUtil(){
super();
}
}
补充知识:java代码实现encodeURIComponent和decodeURIComponent,解决空格转义为加号的问题
java自带有一个java.net.URLDecoder和java.net.URLEncoder。
通过这两个类,可以调用encode()或者decode()方法对字符串进行URL编码。
那既然有了,为什么还要自己实现一套呢?主要原因是Jdk中并没有提供encodeURIComponent和decodeURIComponent的方法。
这两个方法作用其实跟encode()和decode()基本相似。区别主要是,在java中,url编码时,会把空格转换成+号。而某些非java语言实现的客户端一般空格转义出来是%20,这样就容易发生decode不出这个空格的问题。比如IOS中,会把这个+直接显示了,而不是转义成空格。这就跟我们想要的结果违背了。比如js中就自带有encodeURIComponent和decodeURIComponent的方法。
java我们就自己实现一下吧。直接看代码,一看就明白。
/*
*文件名:URIEncode.java描述:修改人:gogym修改时间:2018年11月16日跟踪单号:修改单号:修改内容:
*/
importjava.io.UnsupportedEncodingException;
publicclassURIEncoder
{
publicstaticfinalStringALLOWED_CHARS="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.!~*'()";
/**
*Description:
*
*@paramstr
*@return
*@throwsUnsupportedEncodingException
*@see
*/
publicstaticStringencodeURI(Stringstr)
throwsUnsupportedEncodingException
{
StringisoStr=newString(str.getBytes("UTF8"),"ISO-8859-1");
char[]chars=isoStr.toCharArray();
StringBuffersb=newStringBuffer();
for(inti=0;i='a')||(chars[i]<='Z'&&chars[i]>='A')
||chars[i]=='-'||chars[i]=='_'||chars[i]=='.'||chars[i]=='!'
||chars[i]=='~'||chars[i]=='*'||chars[i]=='\''||chars[i]=='('
||chars[i]==')'||chars[i]==';'||chars[i]=='/'||chars[i]=='?'
||chars[i]==':'||chars[i]=='@'||chars[i]=='&'||chars[i]=='='
||chars[i]=='+'||chars[i]=='$'||chars[i]==','||chars[i]=='#'
||(chars[i]<='9'&&chars[i]>='0'))
{
sb.append(chars[i]);
}
else
{
sb.append("%");
sb.append(Integer.toHexString(chars[i]));
}
}
returnsb.toString();
}
/**
*Description:
*
*@paraminput
*@return
*@see
*/
publicstaticStringencodeURIComponent(Stringinput)
{
if(null==input||"".equals(input.trim()))
{
returninput;
}
intl=input.length();
StringBuildero=newStringBuilder(l*3);
try
{
for(inti=0;i
/*
*文件名:URIDecode.java描述:修改人:gogym修改时间:2018年11月16日跟踪单号:修改单号:修改内容:
*/
packagecom.poly.rbl.plugin.uri;
publicclassURIDecoder
{
/**
*
*Description:
*
*@paramencodedURI
*@return
*@see
*/
publicstaticStringdecodeURIComponent(StringencodedURI)
{
charactualChar;
StringBufferbuffer=newStringBuffer();
intbytePattern,sumb=0;
for(inti=0,more=-1;i
以上这篇java中类似jsencodeURIComponent函数的实现案例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。