将键值对生成网址url

public String getUrl(Map<String, String> map, String valueEnc) {  
       
        if (null == map || map.keySet().size() == 0) {  
            return "";  
        }  
        StringBuffer url = new StringBuffer();  
        Set<String> keys = map.keySet();  
        for (Iterator<String> it = keys.iterator(); it.hasNext();) {  
            String key = it.next();  
            if (map.containsKey(key)) {  
                String val = map.get(key);  
                String str = val != null ? val : "";  
                try {  
                    str = URLEncoder.encode(str, valueEnc);  
                } catch (UnsupportedEncodingException e) {  
                    e.printStackTrace();  
                }  
                url.append(key).append("=").append(str).append("&");  
            }  
        }  
        String strURL = "";  
        strURL = url.toString();  
        if ("&".equals("" + strURL.charAt(strURL.length() - 1))) {  
            strURL = strURL.substring(0, strURL.length() - 1);  
        }  
          
        return (strURL);  
    }

你可能感兴趣的:(url)