web设计者面临的众多难题之一便是怎样处理不同操作系统间的差异性。这些差异性能引起URL方面的问题:例如,一些操作系统允许文件名中含有空格符,有些又不允许。大多数操作系统不会认为文件名中含有符号“#”会有什么特殊含义,但是在一个URL中,符号“#”表示该文件名已经结束,后面会紧跟一个fragment(部分)标识符。
其他的特殊字符,非字母数字字符集,它们在URL或另一个操作系统上都有其特殊的含义,表述着相似的问题。为了解决这些问题,我们在URL中使用的字符就必须是一个ASCII字符集的固定字集中的元素,具体如下:
- 大写字母A-Z
- 小写字母a-z
- 数字 0-9
- 标点符 - _ . ! ~ * ' (,)
诸如字符: / & ? @ # ; $ + = %
也可以被使用,但是它们各有其特殊的用途。当/ # = & 和?作为名字的一部分来使用时,而不是作为URL部分之间的分隔符来使用时,它们都应该被编码。
编码过程非常简单,任何字符只要不是ASCII码数字,字母,或者前面提到的标点符,它们都将被转换成字节形式。每个字节都写成这种形式:一个“%”后面跟着两位16进制的数值。空格是一个特殊情况,因为它们太平常了。它除了被编码成“%20”以外,还能编码为一个“+”。
重要备注:通常只会对URL的参数值进行编码,而不会对整条URL进行编码。
js中的URL编码
js中URL编码最常用的就是encodeURI和encodeURIComponent。如果你需要编码整个URL,然后需要使用这个URL,那么用encodeURI和encodeURIComponent。

对URL编码是常见的事,所以这两个方法应该是实际中要特别注意的。
它们都是编码URL,唯一区别就是编码的字符范围,其中:
- encodeURI方法不会对下列字符编码 :ASCII字母、数字、
~!@#$&*()=:/,;?+'
- encodeURIComponent方法不会对下列字符编码:ASCII字母、数字、
~!*()'
所以encodeURIComponent比encodeURI编码的范围更大。比如
encodeURI("http://www.cnblogs.com/season-huang/some other thing");
编码后会变为:
"http://www.cnblogs.com/season-huang/some%20other%20thing";
其中,空格被编码成了%20。但是如果你用了encodeURIComponent,那么结果变为
"http%3A%2F%2Fwww.cnblogs.com%2Fseason-huang%2Fsome%20other%20thing"
看到了区别吗,连 "/" 都被编码了。
java中的URL编码
【URLEncode】
在java1.3和早期版本中,类java.net.URLEncoder包括一个简单的静态方法encode( ), 它对string以如下规则进行编码:
public static String encode(String s)
这个方法总是用它所在平台的默认编码形式,所以在不同系统上,它就会产生不同的结果。结果java1.4中,这个方法被另一种方法取代了。该方法要求你自己指定编码形式:
public static String encode(String s, String encoding) throws UnsupportedEncodingException
两种关于编码的方法,都把任何非字母数字字符转换成%xx(除了空格(空格被转换成一个加号),下划线(_),连字符(?),句号(。),和星号(*))。两者也都编码所有的非ASCII字符。
下面是示例代码:
import java.net.URLEncoder;
import java.net.URLDecoder;
import java.io.UnsupportedEncodingException;
public class EncoderTest {
public static void main(String[] args) {
try {
System.out.println(URLEncoder.encode("This string has spaces","UTF-8"));
System.out.println(URLEncoder.encode("This*string*has*asterisks","UTF-8"));
System.out.println(URLEncoder.encode("This%string%has%percent%signs", "UTF-8"));
System.out.println(URLEncoder.encode("This+string+has+pluses","UTF-8"));
System.out.println(URLEncoder.encode("This/string/has/slashes","UTF-8"));
System.out.println(URLEncoder.encode("This"string"has"quote"marks", "UTF-8"));
System.out.println(URLEncoder.encode("This:string:has:colons","UTF-8"));
System.out.println(URLEncoder.encode("This~string~has~tildes","UTF-8"));
System.out.println(URLEncoder.encode("This(string)has(parentheses)", "UTF-8"));
System.out.println(URLEncoder.encode("This.string.has.periods","UTF-8"));
System.out.println(URLEncoder.encode("This=string=has=equals=signs", "UTF-8"));
System.out.println(URLEncoder.encode("This&string&has&ersands","UTF-8"));
System.out.println(URLEncoder.encode("Thiséstringéhasé non-ASCII characters","UTF-8"));
// System.out.println(URLEncoder.encode("this中华人民共和国","UTF-8"));
} catch (UnsupportedEncodingException ex) {throw new RuntimeException("
Broken VM does not support UTF-8");
}
}
}
下面就是它的输出:
% javac -encoding UTF8 EncoderTest %
java EncoderTest
This+string+has+spaces
This*string*has*asterisks
This%25string%25has%25percent%25signs
This%2Bstring%2Bhas%2Bpluses
This%2Fstring%2Fhas%2Fslashes
This%22string%22has%22quote%22marks
This%3Astring%3Ahas%3Acolons
This%7Estring%7Ehas%7Etildes
This%28string%29has%28parentheses%29
This.string.has.periods
This%3Dstring%3Dhas%3Dequals%3Dsigns
This%26string%26has%26ampersands
This%C3%A9string%C3%A9has%C3%A9non-ASCII+characters
【URLDecoder】
与URLEncoder 类相对应的URLDecoder 类有两种静态方法。它们解码以x-www-form-url-encoded这种形式编码的string。也就是说,它们把所有的加号(+)转换成空格符,把所有的%xx分别转换成与之相对应的字符:
public static String decode(String s) throws Exception
public static String decode(String s, String encoding) // Java 1.4 throws
UnsupportedEncodingException
第一种解码方法在java1.3和java1.2中使用。第二种解码方法在java1.4和更新的版本中使用。如果你拿不定主意用哪种编码方式,那就选择UTF-8吧。它比其他任何的编码形式更有可能得到正确的结果。
如果string包含了一个“%”,但紧跟其后的不是两位16进制的数或者被解码成非法序列,该方法就会抛出IllegalArgumentException 异常。由于这个方法没有触及到非转义字符,所以你可以把整个URL作为参数传给该方法:
String input = "http://www.altavista.com/cgi-bin/" + "query?
pg=q&kl=XX&stype=stext&q=%2B%22Java+I%2FO%22&search.x=38&search.y=3";
try {
String output = URLDecoder.decode(input, "UTF-8");
System.out.println(output);
}
网友评论