URL在Java编程中的处理

URL解码编码在Java中主要用到java.net包中的两个工具类来处理:
URLDecoder HTML 格式解码的实用工具类。 
URLEncoder HTML 格式编码的实用工具类。
 
下面给个例子:
 
import java.net.URLEncoder; 
import java.net.URLDecoder; 
import java.io.UnsupportedEncodingException; 

/** 
* URL在Java编程中的处理 
* File: TestURL.java 
* User: leizhimin 
* Date: 2008-3-17 16:23:39 
*/
 
public  class TestURL { 
     /** 
     * 将 String 转换为 application/x-www-form-urlencoded MIME 格式的串 
     * @param filepath 要转换的目标的字符串,GBK格式 
     * @return 以UTF-8编码的字符串 
     * @throws UnsupportedEncodingException 
     */
 
     public  static String testURLEncoder(String filepath)  throws UnsupportedEncodingException { 
        String wwwurl = URLEncoder.encode(filepath,  "UTF-8"); 
         return wwwurl; 
    } 

     /** 
     * 将 String 从 application/x-www-form-urlencoded MIME 格式解码为UTF8格式的字符串 
     * @param wwwurl 要转换的目标的字符串,application/x-www-form-urlencoded MIME 格式 
     * @return UTF8格式的字符串 
     * @throws UnsupportedEncodingException 
     */
 
     public  static String testURLDecoder(String wwwurl)  throws UnsupportedEncodingException { 
        String filepath_new = URLDecoder.decode(wwwurl,  "UTF-8"); 
         return filepath_new; 
    } 

     public  static  void main(String args[])  throws UnsupportedEncodingException { 
        String filepath =  "D:\\My Documents\\我接收到的文件\\20_save.gif"
        String wwwurl = testURLEncoder(filepath); 
        String filepath_new = testURLDecoder(wwwurl); 

        System.out.println(filepath); 
        System.out.println(wwwurl); 
        System.out.println(filepath_new); 
    } 
}
 
运行结果:
D:\My Documents\我接收到的文件\20_save.gif 
D%3A%5CMy+Documents%5C%E6%88%91%E6%8E%A5%E6%94%B6%E5%88%B0%E7%9A%84%E6%96%87%E4%BB%B6%5C20_save.gif 
D:\My Documents\我接收到的文件\20_save.gif 

Process finished with exit code 0
 

本文转自 leizhimin 51CTO博客,原文链接:http://blog.51cto.com/lavasoft/66258,如需转载请自行联系原作者
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值