encodeURIComponent 与 decodeURIComponent 编码互转

本文介绍了一个 Java 类 UrlDeal,该类提供了 encodeURIComponent 和 decodeURIComponent 方法用于 URL 的编码与解码。编码方法会将指定字符串转换为 URL 编码格式,而解码方法则实现了将 URL 编码格式的字符串还原为原始字符串。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://yzz9i.iteye.com/blog/1277808

Java代码   收藏代码
  1. package com.file;  
  2.   
  3.   
  4. import java.io.UnsupportedEncodingException;  
  5.    
  6. public class UrlDeal {  
  7.     public static final String ALLOWED_CHARS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.!~*'()";  
  8.    
  9.     public static String encodeURIComponent(String input) {  
  10.         if (input == null || "".equals(input)) {  
  11.             return input;  
  12.         }  
  13.    
  14.         int l = input.length();  
  15.         StringBuilder o = new StringBuilder(l * 3);  
  16.         try {  
  17.             for (int i = 0; i < l; i++) {  
  18.                 String e = input.substring(i, i + 1);  
  19.                 if (ALLOWED_CHARS.indexOf(e) == -1) {  
  20.                     byte[] b = e.getBytes("utf-8");  
  21.                     o.append(getHex(b));  
  22.                     continue;  
  23.                 }  
  24.                 o.append(e);  
  25.             }  
  26.             return o.toString();  
  27.         } catch (UnsupportedEncodingException e) {  
  28.             e.printStackTrace();  
  29.         }  
  30.         return input;  
  31.     }  
  32.    
  33.     private static String getHex(byte buf[]) {  
  34.         StringBuilder o = new StringBuilder(buf.length * 3);  
  35.         for (int i = 0; i < buf.length; i++) {  
  36.             int n = (int) buf[i] & 0xff;  
  37.             o.append("%");  
  38.             if (n < 0x10) {  
  39.                 o.append("0");  
  40.             }  
  41.             o.append(Long.toString(n, 16).toUpperCase());  
  42.         }  
  43.         return o.toString();  
  44.     }  
  45.    
  46.     public static String decodeURIComponent(String encodedURI) {  
  47.         char actualChar;  
  48.    
  49.         StringBuffer buffer = new StringBuffer();  
  50.    
  51.         int bytePattern, sumb = 0;  
  52.    
  53.         for (int i = 0, more = -1; i < encodedURI.length(); i++) {  
  54.             actualChar = encodedURI.charAt(i);  
  55.    
  56.             switch (actualChar) {  
  57.             case '%': {  
  58.                 actualChar = encodedURI.charAt(++i);  
  59.                 int hb = (Character.isDigit(actualChar) ? actualChar - '0'  
  60.                         : 10 + Character.toLowerCase(actualChar) - 'a') & 0xF;  
  61.                 actualChar = encodedURI.charAt(++i);  
  62.                 int lb = (Character.isDigit(actualChar) ? actualChar - '0'  
  63.                         : 10 + Character.toLowerCase(actualChar) - 'a') & 0xF;  
  64.                 bytePattern = (hb << 4) | lb;  
  65.                 break;  
  66.             }  
  67.             case '+': {  
  68.                 bytePattern = ' ';  
  69.                 break;  
  70.             }  
  71.             default: {  
  72.                 bytePattern = actualChar;  
  73.             }  
  74.             }  
  75.    
  76.             if ((bytePattern & 0xc0) == 0x80) { // 10xxxxxx  
  77.                 sumb = (sumb << 6) | (bytePattern & 0x3f);  
  78.                 if (--more == 0)  
  79.                     buffer.append((char) sumb);  
  80.             } else if ((bytePattern & 0x80) == 0x00) { // 0xxxxxxx  
  81.                 buffer.append((char) bytePattern);  
  82.             } else if ((bytePattern & 0xe0) == 0xc0) { // 110xxxxx  
  83.                 sumb = bytePattern & 0x1f;  
  84.                 more = 1;  
  85.             } else if ((bytePattern & 0xf0) == 0xe0) { // 1110xxxx  
  86.                 sumb = bytePattern & 0x0f;  
  87.                 more = 2;  
  88.             } else if ((bytePattern & 0xf8) == 0xf0) { // 11110xxx  
  89.                 sumb = bytePattern & 0x07;  
  90.                 more = 3;  
  91.             } else if ((bytePattern & 0xfc) == 0xf8) { // 111110xx  
  92.                 sumb = bytePattern & 0x03;  
  93.                 more = 4;  
  94.             } else { // 1111110x  
  95.                 sumb = bytePattern & 0x01;  
  96.                 more = 5;  
  97.             }  
  98.         }  
  99.         return buffer.toString();  
  100.     }  
  101.     public static void main(String[] arges){  
  102.         System.out.println(decodeURIComponent("%E4%BD%A0%E5%A5%BD%20%E7%9C%9F%E7%9A%84"));  
  103.         System.out.println(encodeURIComponent("真的"));  
  104.         System.out.println("%E4%BD%A0%E5%A5%BD%20%E7%9C%9F%E7%9A%84");  
  105.     }  
  106. }  
0 
0 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值