一、前言
关于flex-webtier源码包中flex.webtier.server.j2ee.URLEncoder、flex.webtier.server.j2ee.URLDecoder、flex.webtier.server.j2ee.URLHelper的URL资源及加解密工具类,这边主要URLHelper提供出获取getQueryString链接访问路径、将传入参数转换为Map处理getParameterMap、对入参进行加密编码处理encode、对url的伪码处理escapeSpace等
二、源码说明
1.URLHelper类
![]() 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | package flex.webtier.server.j2ee; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Map.Entry; import java.util.Set; import java.util.StringTokenizer; public class URLHelper { public static String getQueryString(String url) { String queryString = null ; if (url != null ) { int queryMark = url.indexOf( 63 ); if (queryMark != - 1 ) queryString = url.substring(queryMark + 1 ); } return queryString; } public static Map getParameterMap(String queryString) { Map map; if (queryString != null ) { StringTokenizer tokens = new StringTokenizer(queryString, "?&" ); map = new HashMap(tokens.countTokens() * 2 ); while (tokens.hasMoreElements()) { String nameValuePair = tokens.nextToken(); String name = nameValuePair; String value = "" ; int equalsIndex = nameValuePair.indexOf( 61 ); if (equalsIndex != - 1 ) { name = nameValuePair.substring( 0 , equalsIndex); if (name.length() > 0 ) value = nameValuePair.substring(equalsIndex + 1 ); } map.put(name, value); } } else { map = new HashMap(); } return map; } public static String encode(Map parameterMap) { return encode(parameterMap, false ); } public static String encode(Map parameterMap, boolean quoteAmpersands) { if ((parameterMap != null ) && (!(parameterMap.isEmpty()))) { StringBuffer queryString = new StringBuffer(); Iterator it = parameterMap.entrySet().iterator(); while (it.hasNext()) { Map.Entry entry = (Map.Entry)it.next(); String name = (String)entry.getKey(); String value = String.valueOf(entry.getValue()); queryString.append(URLEncoder.encode(name)); if ((value != null ) && (!(value.equals( "" )))) { queryString.append( '=' ); queryString.append(URLEncoder.encode(value)); } if (it.hasNext()) queryString = (quoteAmpersands) ? queryString.append( "\"&\"" ) : queryString.append( '&' ); } return queryString.toString(); } return null ; } public static String escapeSpace(String uri) { return escapeCharacter(uri, ' ' , "%20" ); } public static String escapeCharacter(String uri, char c, String to) { int i; StringBuffer sb = new StringBuffer(); int size = uri.length(); int at = uri.indexOf( 58 ); int lastAt = 0 ; if (at > - 1 ) { for (i = 0 ; i <= at; ++i) sb.append(uri.charAt(i)); lastAt = ++at; } while ((at = uri.indexOf(c, at)) > - 1 ) { for (i = lastAt; i < at; ++i) { sb.append(uri.charAt(i)); } sb.append(to); lastAt = ++at; } if (lastAt < size) { for (i = lastAt; i < size; ++i) sb.append(uri.charAt(i)); } return sb.toString(); } } |
2.URLEncoder、URLDecoder类
![]() 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 | package flex.webtier.server.j2ee; import java.io.BufferedWriter; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.UnsupportedEncodingException; public final class URLEncoder { public static final String charset = "UTF8" ; public static String encode(String s) { try { return encode(s, "UTF8" ); } catch (UnsupportedEncodingException ex) { throw new IllegalArgumentException( "UTF8" ); } } public static String encode(String s, String enc) throws UnsupportedEncodingException { if (!(needsEncoding(s))) { return s; } int length = s.length(); StringBuffer out = new StringBuffer(length); ByteArrayOutputStream buf = new ByteArrayOutputStream( 10 ); BufferedWriter writer = new BufferedWriter( new OutputStreamWriter(buf, enc)); for ( int i = 0 ; i < length; ++i) { int c = s.charAt(i); if (((c >= 97 ) && (c <= 122 )) || ((c >= 65 ) && (c <= 90 )) || ((c >= 48 ) && (c <= 57 )) || (c == 32 )) { if (c == 32 ) { c = 43 ; } toHex(out, buf.toByteArray()); buf.reset(); out.append(( char )c); } else { try { writer.write(c); if ((c >= 55296 ) && (c <= 56319 ) && (i < length - 1 )) { int d = s.charAt(i + 1 ); if ((d >= 56320 ) && (d <= 57343 )) { writer.write(d); ++i; } } writer.flush(); } catch (IOException ex) { throw new IllegalArgumentException(s); } } } toHex(out, buf.toByteArray()); return out.toString(); } private static void toHex(StringBuffer buffer, byte [] b) { for ( int i = 0 ; i < b.length; ++i) { buffer.append( '%' ); char ch = Character.forDigit(b[i] >> 4 & 0xF , 16 ); if (Character.isLetter(ch)) { ch = ( char )(ch - ' ' ); } buffer.append(ch); ch = Character.forDigit(b[i] & 0xF , 16 ); if (Character.isLetter(ch)) { ch = ( char )(ch - ' ' ); } buffer.append(ch); } } private static boolean needsEncoding(String s) { if (s == null ) { return false ; } int length = s.length(); for ( int i = 0 ; i < length; ++i) { int c = s.charAt(i); if ((((c < 97 ) || (c > 122 ))) && (((c < 65 ) || (c > 90 )))) { if ((c >= 48 ) && (c <= 57 )) { break label65: } return true ; } } label65: return false ; } } |
![]() 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 | package flex.webtier.server.j2ee; import java.io.UnsupportedEncodingException; public final class URLDecoder { public static String decode(String s) { try { return decode(s, "UTF8" ); } catch (UnsupportedEncodingException ex) { throw new IllegalArgumentException( "UTF8" ); } } public static String decode(String s, String enc) throws UnsupportedEncodingException { if (!(needsDecoding(s))) { return s; } int length = s.length(); byte [] bytes = new byte [length]; s.getBytes( 0 , length, bytes, 0 ); int k = 0 ; length = bytes.length; for ( int i = 0 ; i < length; ++i) { if (bytes[i] == 37 ) { while (bytes[(i + 1 )] == 37 ) { ++i; } if (i < length - 2 ) { bytes[k] = x2c(bytes, i); i += 2 ; break label126: } throw new IllegalArgumentException(s); } if (bytes[i] == 43 ) { bytes[k] = 32 ; } else { bytes[k] = bytes[i]; } ++k; } label126: return new String(bytes, 0 , k, enc); } private static boolean needsDecoding(String s) { if (s == null ) { return false ; } int length = s.length(); for ( int i = 0 ; i < length; ++i) { int c = s.charAt(i); if ((c == 43 ) || (c == 37 )) { return true ; } } return false ; } private static byte x2c( byte [] b, int i) { byte b1 = b[(i + 1 )]; byte b2 = b[(i + 2 )]; if ((b1 < 48 ) || ((b1 > 70 ) && (b1 < 97 )) || (b1 > 102 ) || (b2 < 48 ) || ((b2 > 70 ) && (b2 < 97 )) || (b2 > 102 )) { throw new IllegalArgumentException( "%" + ( char )b1 + ( char )b2); } int result = (b1 >= 65 ) ? (b1 & 0xDF ) - 65 + 10 : b1 - 48 ; result *= 16 ; result += ((b2 >= 65 ) ? (b2 & 0xDF ) - 65 + 10 : b2 - 48 ); return ( byte )result; } } |