b2科目四模拟试题多少题驾考考爆了怎么补救
b2科目四模拟试题多少题 驾考考爆了怎么补救

urldecoder.decode_linux urldecode_urlencode的作用(2)

电脑杂谈  发布时间:2017-04-08 01:02:56  来源:网络整理

public class URLEncoder extends Object

一、URLEncoder

在java1.3和早期版本中,类java.net.URLEncoder包括一个简单的静态方法encode( ), 它对string以如下规则进行编码:

public static String encode(String s)

这个方法总是用它所在平台的默认编码形式,所以在不同系统上,它就会产生不同的结果。结果java1.4中,这个方法被另一种方法取代了。urldecoder.decode该方法要求你自己指定编码形式:

public static String encode(String s, String encoding) throws UnsupportedEncodingException

两种关于编码的方法,都把任何非字母数字字符转换成%xx(除了空,下划线(_),连字符(?),句号(。),和星号(*))。两者也都编码所以的非ASCII字符。空被转换成一个加号。这些方法有一点过分累赘了;它们也把“~”,“‘”,“()”转换成%xx,即使它们完全用不着这样做。尽管这样,但是这种转换并没被URL规范所禁止。所以web浏览器会自然地处理这些被过分编码后的URL。

两中关于编码的方法都返回一个新的被编码后的string,java1.3的方法encode( ) 使用了平台的默认编码形式,得到%xx。这些编码形式典型的有:在 U.S. Unix 系统上的ISO-8859-1, 在U.S. Windows 系统上的Cp1252,在U.S. Macs上的MacRoman,和其他本地字符集等。因为编码解码过程都是与本地操作平台相关的,所以这些方法是令人不爽的,不能跨平台的。

这就明确地回答了为什么在java1.4中这种方法被抛弃了,转而投向了要求以自己指定编码形式的这种方法。尽管如此,如果你执意要使用所在平台的默认编码形式,你的程序将会像在java1.3中的程序一样,是本地平台相关的。在另一种编码的方法中,你应该总是用UTF-8,而不是其他什么。UTF-8比起你选的其他的编码形式来说,它能与新的web浏览器和更多的其他软件相兼容。

例子7-8是使用URLEncoder.encode( ) 来打印输出各种被编码后的string。它需要在java1.4或更新的版本中编译和运行。

Example 7-8. x--form-urlencoded strings

<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)

http://.CodeHighlighter.com/

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


本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-40100-2.html

相关阅读
    发表评论  请自觉遵守互联网相关的政策法规,严禁发布、暴力、反动的言论

    每日福利
    热点图片
    拼命载入中...