使用URLEncoder和URLDecoder

原文地址:http://www.blogjava.net/aoelwh/articles/URLEncoderURLDecoder.html

如果URL里面要包含特殊的字符和空格,你需要对其进行编码,你需要编码和解码。下面的代码演示了如何进行编码和解码。

 import java.net.*;

            public class URL_Encoder_Decoder{

            public static void main(String[] args){

              String sir="@sir de caractere@,nr. 1290 'paragraf 3'";
              try{
                 String sir_codat=URLEncoder.encode(sir,"UTF-8");
                 String sir_decodat=URLDecoder.decode(sir_codat,"UTF-8");
                 System.out.println(sir);
                 System.out.println(sir_codat);  
                 System.out.println(sir_decodat);      
                 }catch(java.io.UnsupportedEncodingException e)
                     {System.out.println("Eroare:"+e.getMessage());}            
              }
            }



下面是输出结果:
            @sir de caractere@,nr. 1290 'paragraf 3'
            %40sir+de+caractere%40%2Cnr.+1290+%27paragraf+3%27
            @sir de caractere@,nr. 1290 'paragraf 3'

你可能感兴趣的:(java,html,.net)