亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

java 中類似js encodeURIComponent 函數的實現案例

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

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

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) { // 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 函數的實現案例就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持好吧啦網。

標簽: Java
相關文章:
主站蜘蛛池模板: 成人无遮挡免费网站视频在线观看 | 欧美v亚洲v国产v | 古代级a毛片可以免费看 | 丝袜网站在线观看 | 亚洲精品国产电影午夜 | 99精品国产自在现线观看 | 99av在线| 成人影片在线播放 | 亚洲成人99 | 亚洲性一级理论片在线观看 | 亚洲伦理精品久久 | 天天欲色成人综合网站 | 免费看的毛片 | 沈樵在线观看国产专区 | 91短视频版官网 | 国产精品美女www爽爽爽视频 | 免费看精品黄线在线观看 | 亚洲无线乱码高清在线观看一区 | 亚洲精品日本高清中文字幕 | 久久精品国产夜色 | 婷婷亚洲天堂 | 国产毛片自拍 | 久久97精品久久久久久久看片 | 伊人中文字幕在线观看 | 在线观看国产日本 | 国产大乳喷奶水在线看 | 免费看国产做爰大片 | 又黄又爽又成人免费视频 | 欧美成人午夜免费完成 | 日本 亚洲 欧美 | 久久99热在线观看7 久久99热只有视精品6国产 | 亚洲欧美日韩在线精品2021 | 天天鲁天天玩天天爽天天 | 中文字幕日韩欧美一区二区三区 | 国产成人精品亚洲午夜麻豆 | 国产一级特黄特色aa毛片 | 爱爱免费播放视频在线观看 | 日韩欧美不卡片 | 成人免费视频视频在线不卡 | 永久免费看的啪啪网站 | 欧美一区二区手机在线观看视频 |