java 中类似js encodeURIComponent 函数的实现案例
作者:wangweiren_get 时间:2023-03-20 13:13:50
我就废话不多说了,大家还是直接看代码吧~
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
/**
* Utility class for JavaScript compatible UTF-8 encoding and decoding.
*
* @see http://stackoverflow.com/questions/607176/java-equivalent-to-javascripts-encodeuricomponent-that-produces-identical-output
* @author John Topley
*/
public class EncodingUtil {
/**
* Decodes the passed UTF-8 String using an algorithm that's compatible with
* JavaScript's <code>decodeURIComponent</code> function. Returns
* <code>null</code> if the String is <code>null</code>.
*
* @param s The UTF-8 encoded String to be decoded
* @return the decoded String
*/
public static String decodeURIComponent(String s) {
if (s == null) {
return null;
}
String result = null;
try {
result = URLDecoder.decode(s, "UTF-8");
}
// This exception should never occur.
catch (UnsupportedEncodingException e) {
result = s;
}
return result;
}
/**
* Encodes the passed String as UTF-8 using an algorithm that's compatible
* with JavaScript's <code>encodeURIComponent</code> function. Returns
* <code>null</code> if the String is <code>null</code>.
*
* @param s The String to be encoded
* @return the encoded String
*/
public static String encodeURIComponent(String s) {
String result = null;
try {
result = URLEncoder.encode(s, "UTF-8")
.replaceAll("\\+", "%20")
.replaceAll("\\%21", "!")
.replaceAll("\\%27", "'")
.replaceAll("\\%28", "(")
.replaceAll("\\%29", ")")
.replaceAll("\\%7E", "~");
}
// This exception should never occur.
catch (UnsupportedEncodingException e) {
result = s;
}
return result;
}
/**
* Private constructor to prevent this class from being instantiated.
*/
private EncodingUtil() {
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日 跟踪单号: 修改单号: 修改内容:
*/
import java.io.UnsupportedEncodingException;
public class URIEncoder
{
public static final String ALLOWED_CHARS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.!~*'()";
/**
* Description:
*
* @param str
* @return
* @throws UnsupportedEncodingException
* @see
*/
public static String encodeURI(String str)
throws UnsupportedEncodingException
{
String isoStr = new String(str.getBytes("UTF8"), "ISO-8859-1");
char[] chars = isoStr.toCharArray();
StringBuffer sb = new StringBuffer();
for (int i = 0; i < chars.length; i++ )
{
if ((chars[i] <= 'z' && chars[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]));
}
}
return sb.toString();
}
/**
* Description:
*
* @param input
* @return
* @see
*/
public static String encodeURIComponent(String input)
{
if (null == input || "".equals(input.trim()))
{
return input;
}
int l = input.length();
StringBuilder o = new StringBuilder(l * 3);
try
{
for (int i = 0; i < l; i++ )
{
String e = input.substring(i, i + 1);
if (ALLOWED_CHARS.indexOf(e) == -1)
{
byte[] b = e.getBytes("utf-8");
o.append(getHex(b));
continue;
}
o.append(e);
}
return o.toString();
}
catch (UnsupportedEncodingException e)
{
e.printStackTrace();
}
return input;
}
private static String getHex(byte buf[])
{
StringBuilder o = new StringBuilder(buf.length * 3);
for (int i = 0; i < buf.length; i++ )
{
int n = (int)buf[i] & 0xff;
o.append("%");
if (n < 0x10)
{
o.append("0");
}
o.append(Long.toString(n, 16).toUpperCase());
}
return o.toString();
}
}
/*
* 文件名:URIDecode.java 描述: 修改人:gogym 修改时间:2018年11月16日 跟踪单号: 修改单号: 修改内容:
*/
package com.poly.rbl.plugin.uri;
public class URIDecoder
{
/**
*
* Description:
*
* @param encodedURI
* @return
* @see
*/
public static String decodeURIComponent(String encodedURI)
{
char actualChar;
StringBuffer buffer = new StringBuffer();
int bytePattern, sumb = 0;
for (int i = 0, more = -1; i < encodedURI.length(); i++ )
{
actualChar = encodedURI.charAt(i);
switch (actualChar)
{
case '%':
{
actualChar = encodedURI.charAt(++i);
int hb = (Character.isDigit(actualChar) ? actualChar - '0' : 10 + Character.toLowerCase(actualChar) - 'a') & 0xF;
actualChar = encodedURI.charAt(++i);
int lb = (Character.isDigit(actualChar) ? actualChar - '0' : 10 + Character.toLowerCase(actualChar) - 'a') & 0xF;
bytePattern = (hb << 4) | lb;
break;
}
case '+':
{
bytePattern = ' ';
break;
}
default:
{
bytePattern = actualChar;
}
}
if ((bytePattern & 0xc0) == 0x80)
{ // 10xxxxxx
sumb = (sumb << 6) | (bytePattern & 0x3f);
if (--more == 0) buffer.append((char)sumb);
}
else if ((bytePattern & 0x80) == 0x00)
{ // 0xxxxxxx
buffer.append((char)bytePattern);
}
else if ((bytePattern & 0xe0) == 0xc0)
{ // 110xxxxx
sumb = bytePattern & 0x1f;
more = 1;
}
else if ((bytePattern & 0xf0) == 0xe0)
{ // 1110xxxx
sumb = bytePattern & 0x0f;
more = 2;
}
else if ((bytePattern & 0xf8) == 0xf0)
{ // 11110xxx
sumb = bytePattern & 0x07;
more = 3;
}
else if ((bytePattern & 0xfc) == 0xf8)
{ // 111110xx
sumb = bytePattern & 0x03;
more = 4;
}
else
{ // 1111110x
sumb = bytePattern & 0x01;
more = 5;
}
}
return buffer.toString();
}
}
来源:https://blog.csdn.net/wangweiren_get/article/details/82585629
标签:java,js,encodeURIComponent函数
0
投稿
猜你喜欢
C#抽象类和接口的区别分析
2023-11-09 13:56:21
关于springcloud集成nacos遇到的问题
2022-02-27 17:41:46
c#中使用BackgroundWorker的实现
2023-05-04 08:51:01
浅谈C#设计模式之代理模式
2023-01-23 14:30:59
Java8新特性之泛型的目标类型推断_动力节点Java学院整理
2023-11-26 10:38:21
Spring中自定义数据类型转换的方法详解
2022-10-09 02:56:51
Java调用groovy实现原理代码实例
2023-05-16 16:41:51
android panellistview 圆角实现代码
2022-09-03 17:34:19
java中ConcurrentHashMap的读操作为什么不需要加锁
2021-10-07 18:30:44
Object类toString()和equals()方法使用解析
2022-10-28 08:48:43
Unity3D实现物体旋转缩放移动效果
2023-07-17 22:11:58
springboot自定义异常视图过程解析
2023-06-29 09:44:36
Android仿美团分类下拉菜单实例代码
2022-09-04 09:59:02
java简单实现斗地主发牌功能
2023-06-18 16:22:44
C#处理Paint事件的方法
2022-01-12 02:02:51
Android巧用ActionBar实现tab导航效果
2022-06-02 05:49:18
Java多线程下解决数据安全问题
2022-05-16 04:48:45
Kotlin之在Gradle中无参(no-arg)编译器插件的使用详解
2023-07-31 19:11:43
Android控件PopupWindow模仿ios底部弹窗
2023-03-09 20:42:10
MyBatisPlus 大数据量查询慢的问题解决
2022-06-17 05:56:39