集册 Java实例教程 解码要映射的给定URL查询字符串。

解码要映射的给定URL查询字符串。

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

369
解码给定的URL查询字符串以进行映射。


//package com.nowjava;
/**
n  o  w  j  a  v  a . c o m 提供 
**/


import java.io.UnsupportedEncodingException;


import java.net.URLDecoder;


import java.util.HashMap;

import java.util.Map;


public class Main {



    /**

     * Decode given String to map. For example for input: accessToken=123456&expires=20071458 it returns map with two keys

     * "accessToken" and "expires" and their corresponding values

     *

     * @param encodedData

     * @return map with output data

     */

    public static Map<String, String> formUrlDecode(String encodedData) {

        Map<String, String> params = new HashMap<String, String>();

        String[] elements = encodedData.split("&");

        for (String element : elements) {

            String[] pair = element.split("=");

            if (pair.length == 2) {/** 时 代      J a v a   公   众 号 - nowjava.com 提 供 **/

                String paramName = pair[0];

                String paramValue;

                try {

                    paramValue = URLDecoder.decode(pair[1], "UTF-8");

                } catch (UnsupportedEncodingException e) {

                    throw new RuntimeException(e);

                }

                params.put(paramName, paramValue);

            } 
展开阅读全文