国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

java 中類似js encodeURIComponent 函數(shù)的實(shí)現(xiàn)案例

瀏覽:4日期:2022-08-23 09:32:54

我就廢話不多說(shuō)了,大家還是直接看代碼吧~

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(); }}

補(bǔ)充知識(shí):java 代碼實(shí)現(xiàn)encodeURIComponent和decodeURIComponent,解決空格轉(zhuǎn)義為加號(hào)的問(wèn)題

java自帶有一個(gè) java.net.URLDecoder和java.net.URLEncoder。

通過(guò)這兩個(gè)類,可以調(diào)用encode()或者decode()方法對(duì)字符串進(jìn)行URL編碼。

那既然有了,為什么還要自己實(shí)現(xiàn)一套呢?主要原因是Jdk中并沒(méi)有提供encodeURIComponent和decodeURIComponent的方法。

這兩個(gè)方法作用其實(shí)跟encode()和decode()基本相似。區(qū)別主要是,在java中,url編碼時(shí),會(huì)把空格轉(zhuǎn)換成+號(hào)。而某些非java語(yǔ)言實(shí)現(xiàn)的客戶端一般空格轉(zhuǎn)義出來(lái)是 %20 ,這樣就容易發(fā)生decode不出這個(gè)空格的問(wèn)題。比如IOS中,會(huì)把這個(gè)+直接顯示了,而不是轉(zhuǎn)義成空格。這就跟我們想要的結(jié)果違背了。比如js中就自帶有encodeURIComponent和decodeURIComponent的方法。

java我們就自己實(shí)現(xiàn)一下吧。直接看代碼,一看就明白。

/* * 文件名:URIEncode.java 描述: 修改人:gogym 修改時(shí)間:2018年11月16日 跟蹤單號(hào): 修改單號(hào): 修改內(nèi)容: */ 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 修改時(shí)間:2018年11月16日 跟蹤單號(hào): 修改單號(hào): 修改內(nèi)容: */ 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) { // 10xxxxxxsumb = (sumb << 6) | (bytePattern & 0x3f);if (--more == 0) buffer.append((char)sumb); } else if ((bytePattern & 0x80) == 0x00) { // 0xxxxxxxbuffer.append((char)bytePattern); } else if ((bytePattern & 0xe0) == 0xc0) { // 110xxxxxsumb = bytePattern & 0x1f;more = 1; } else if ((bytePattern & 0xf0) == 0xe0) { // 1110xxxxsumb = bytePattern & 0x0f;more = 2; } else if ((bytePattern & 0xf8) == 0xf0) { // 11110xxxsumb = bytePattern & 0x07;more = 3; } else if ((bytePattern & 0xfc) == 0xf8) { // 111110xxsumb = bytePattern & 0x03;more = 4; } else { // 1111110xsumb = bytePattern & 0x01;more = 5; } } return buffer.toString(); }}

以上這篇java 中類似js encodeURIComponent 函數(shù)的實(shí)現(xiàn)案例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Java
相關(guān)文章:
主站蜘蛛池模板: 怡红院在线视频观看 | 亚洲不卡视频在线观看 | 国产或人精品日本亚洲77美色 | 成年日韩片av在线网站 | 午夜国产精品久久久久 | 亚洲欧美精品国产一区色综合 | 性欧美videos俄罗斯 | 亚洲 中文 欧美 日韩 在线人 | 欧美片a| 三级黄色高清视频 | 亚洲一区二区三区不卡在线播放 | a国产视频 | 亚洲欧洲一区二区 | 99re热视频这里只精品 | 欧美成人免费全部色播 | 男人好大好硬好爽免费视频 | 国产成人艳妇在线观看 | 九九色网站 | 欧美国产成人一区二区三区 | 中国成人免费视频 | 日本精品1在线区 | 欧美精品1 | 在线视频亚洲欧美 | 欧美成人观看视频在线 | 国产成人在线看 | 久久久亚洲精品国产 | 精品日韩一区二区三区视频 | 日韩免费一区二区三区在线 | 狠狠88综合久久久久综合网 | 国产欧美久久久另类精品 | 一级特黄aaa大片在线观看 | 泰国一级毛片aaa下面毛多 | 久久免费视频精品 | 国产日韩欧美视频 | 九九全国免费视频 | 国产精品人成 | 国产三级在线播放线 | 欧美日本一道道一区二区三 | 精品国产品香蕉在线观看 | 长腿嫩模打开双腿呻吟 | av成人天堂 |