集册 Java实例教程 用自己的算法解码URL

用自己的算法解码URL

欢马劈雪     最近更新时间:2020-01-02 10:19:05

328
使用您自己的算法解码URL
/** 来自 时   代    Java - nowjava.com**/


//package com.nowjava;

import java.io.UnsupportedEncodingException;

import java.net.URLDecoder;

import java.util.StringTokenizer;


public class Main {

    public static String decodeURL(String s) {

        if (s == null) {

            return "";

        }

        if (s.indexOf('+') < 0) {

            try {

                return URLDecoder.decode(s, "UTF-8");

            } catch (IllegalArgumentException e) {

                e.printStackTrace();

                return "";
                /** 
                 来自 nowjava.com - 时  代  Java**/

            } catch (UnsupportedEncodingException e) {

                e.printStackTrace();

                return "";

            }

        }

        StringTokenizer st = new StringTokenizer(s, "+", true);

        StringBuilder sb = new StringBuilder();

        while (st.hasMoreTokens()) {

            String tk = st.nextToken();

            if ("+".equals(tk)) {

                sb.append("+");

            } else {

                try {

                    tk = URLDecoder.decode(tk, "UTF-8");

                } 
展开阅读全文