UrlUtils工具类,Java URL工具类,Java URL链接工具类

UrlUtils工具类,Java URL工具类,Java URL链接工具类

 

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

©Copyright 蕃薯耀 2017年7月15日

http://fanshuyao.iteye.com/

 

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class UrlUtils {

	/**
	 * 向url链接追加参数
	 * @param url
	 * @param params Map
	 * @return
	 */
	public static String appendParams(String url, Map params){
		if(StrUtils.isBlank(url)){
			return "";
		}else if(StrUtils.isEmptyMap(params)){
			return url.trim();
		}else{
			StringBuffer sb = new StringBuffer("");
			Set keys = params.keySet();
			for (String key : keys) {
				sb.append(key).append("=").append(params.get(key)).append("&");
			}
			sb.deleteCharAt(sb.length() - 1);
			
			url = url.trim();
			int length = url.length();
			int index = url.indexOf("?");
			if(index > -1){//url说明有问号
				if((length - 1) == index){//url最后一个符号为?,如:http://wwww.baidu.com?
					url += sb.toString();
				}else{//情况为:http://wwww.baidu.com?aa=11
					url += "&" + sb.toString();
				}
			}else{//url后面没有问号,如:http://wwww.baidu.com
				url += "?" + sb.toString();
			}
			return url;
		}
	}
	
	/**
	 * 向url链接追加参数(单个)
	 * @param url
	 * @param name String
	 * @param value String
	 * @return
	 */
	public static String appendParam(String url, String name, String value){
		if(StrUtils.isBlank(url)){
			return "";
		}else if(StrUtils.isBlank(name)){
			return url.trim();
		}else{
			Map params = new HashMap();
			params.put(name, value);
			return appendParams(url, params);
		}
	}
	
	/**
	 * 移除url链接的多个参数
	 * @param url String
	 * @param paramNames String[]
	 * @return
	 */
	public static String removeParams(String url, String... paramNames){
		if(StrUtils.isBlank(url)){
			return "";
		}else if(StrUtils.isEmptyArray(paramNames)){
			return url.trim();
		}else{
			url = url.trim();
			int length = url.length();
			int index = url.indexOf("?");
			if(index > -1){//url说明有问号
				if((length - 1) == index){//url最后一个符号为?,如:http://wwww.baidu.com?
					return url;
				}else{//情况为:http://wwww.baidu.com?aa=11或http://wwww.baidu.com?aa=或http://wwww.baidu.com?aa
					String baseUrl = url.substring(0, index);
					String paramsString = url.substring(index + 1);
					String[] params = paramsString.split("&");
					if(!StrUtils.isEmptyArray(params)){
						Map paramsMap = new HashMap();
						for (String param : params) {
							if(!StrUtils.isBlank(param)){
								String[] oneParam = param.split("=");
								String paramName = oneParam[0];
								int count = 0;
								for(int i=0; i 1)?oneParam[1]:"");
								}
							}
						}
						if(!StrUtils.isEmptyMap(paramsMap)){
							StringBuffer paramBuffer = new StringBuffer(baseUrl);
							paramBuffer.append("?");
							Set set = paramsMap.keySet();
							for (String paramName : set) {
								paramBuffer.append(paramName).append("=").append(paramsMap.get(paramName)).append("&");
							}
							paramBuffer.deleteCharAt(paramBuffer.length() - 1);
							return paramBuffer.toString();
						}
						return baseUrl;
					}
				}
			}
			return url;
		}
	}
	
	public static void main(String[] args) {
		/*String a = "http://wwww.baidu.com";
		String b = "http://wwww.baidu.com?";
		String c = "http://wwww.baidu.com?aa=11";
		System.out.println("a="+appendParam(a, "bb", "1"));
		System.out.println("b="+appendParam(b, "bb", "1"));
		System.out.println("c="+appendParam(c, "bb", "1"));*/
		
		String d = "http://wwww.baidu.com?aa";
		String e = "http://wwww.baidu.com?aa=11&bb=22&cc=33";
		String f = "http://wwww.baidu.com?aa=11&bb=22&cc=33&dd=";
		String g = "http://wwww.baidu.com?aa=11&bb=22&cc=33&dd";
		System.out.println("g="+removeParams(g, "cc","aa"));
	}
}

 

StrUtils 工具类:

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;
import java.util.UUID;


public class StrUtils {
	
	/**
	 * 随机获取数字和大写英文字母组合的字符串
	 * @param size 返回的字符串的位数,如果小于1,则默认是6
	 * @return String
	 * @since 2015-09-25
	 */
	 public static String getRandomLetterAndDigital(int size){
		String str = "23456789ABCDEFGHJKLMNPQRSTUVWXYZ";//去掉容易混淆字符:0与O,1与I
		StringBuffer sb = new StringBuffer();
		if(size < 1){
			size = 6;
		}
		for(int i=0; i -1) {  
                return str.substring(0, str.indexOf(spiltCode)).trim();  
            }
        }
        return str;  
    }
    
    /** 
     * 去相同部分
     * @param originalStr 原字符串 
     * @param deleteStr 需要去掉的字符串 
     * @return string 
     * @author lqy 
     */  
    public static String removeSamePart(String originalStr, String deleteStr) {  
        if (originalStr != null && deleteStr != null) {  
            originalStr = originalStr.replaceAll("\\(", "(");  
            originalStr = originalStr.replaceAll("\\)", ")");  
            originalStr = originalStr.replaceAll(" | ", "");  
            deleteStr = deleteStr.replaceAll("\\(", "(");  
            deleteStr = deleteStr.replaceAll("\\)", ")");  
            deleteStr = deleteStr.replaceAll(" | ", "");  
            if (originalStr.indexOf(deleteStr) > -1) {  
                originalStr = originalStr.replaceAll(deleteStr, "");  
            }  
        }  
        return originalStr;  
    }  
    
    /** 
     * 拆分字符串获取数组 
     * @param str 字符串 
     * @param spiltCode 拆分符号 
     * @return String[] 
     */  
    public static String[] getArrayAfterSpilt(String str, String spiltCode) {  
        if (str == null || str.trim().equals("")) {  
            return null;  
        }else{  
            if (spiltCode == null || spiltCode.trim().equals("")) {  
                spiltCode = ",";  
            }  
            return str.split(spiltCode);  
        }  
    } 
    
    /** 
     * 拆分字符串获取Ids 
     * @param idsString id字符串 
     * @param spiltCode 拆分符号 
     * @return ids 
     */  
    public static int[] getIdsAfterSpilt(String idsString, String spiltCode) {  
        List idList = new ArrayList();  
        if (idsString == null || idsString.trim().equals("")) {  
            return null;  
        } else {  
            if (spiltCode == null || spiltCode.trim().equals("")) {  
                spiltCode = ",";  
            }  
            String[] idArray = idsString.split(spiltCode);  
            if (idArray != null && idArray.length > 0) {  
                for (String string : idArray) {  
                    if (string != null && !string.trim().equals("")) {  
                        idList.add(Integer.parseInt(string.trim()));  
                    }  
                }  
            }  
        }  
        if (idList != null && idList.size() > 0) {  
            int[] ids = new int[idList.size()];  
            for (int j = 0; j < idList.size(); j++) {  
                ids[j] = idList.get(j);  
            }  
            return ids;  
        }  
        return null;  
    }  
	
    /**
	 * 
	 * @param obj
	 * @return obj == null;
	 */
	public static boolean isNull(Object obj) {
        return obj == null;
    }
	
	
	/**
	 * 判断list是否为Null
	 * @param list
	 * @return
	 */
	public static  boolean isNullList(List list) {
        return (list == null);
    }
	
	/**
	 * 判断list是否为空
	 * @param list
	 * @return (list == null) || (list.size() < 1)
	 */
	public static  boolean isEmptyList(List list) {
        return (list == null) || (list.size() < 1);
    }
	
	/**
	 * 判断Map是否为Null
	 * @param map
	 * @return
	 */
	public static  boolean isNullMap(Map map) {
        return (map == null);
    }
	
	/**
	 * 判断Map是否为空
	 * @param map
	 * @return
	 */
	public static  boolean isEmptyMap(Map map) {
        return (map == null || map.size() < 1);
    }
	
	/**
	 * 判断数组是否为Null
	 * @param obj
	 * @return
	 */
	public static boolean isNullArray(Object[] obj) {
        return (obj == null);
    }
	/**
	 * 判断数组是否为空
	 * @param obj
	 * @return
	 */
	public static boolean isEmptyArray(Object[] obj) {
        return (obj == null || obj.length < 1);
    }
	
	/**
     * 

Checks if a String is empty ("") or null.

* *
     * StringUtils.isEmpty(null)      = true
     * StringUtils.isEmpty("")        = true
     * StringUtils.isEmpty(" ")       = false
     * StringUtils.isEmpty("bob")     = false
     * StringUtils.isEmpty("  bob  ") = false
     * 
* *

NOTE: This method changed in Lang version 2.0. * It no longer trims the String. * That functionality is available in isBlank().

* * @param str the String to check, may be null * @return true if the String is empty or null */ public static boolean isEmpty(String str) { return str == null || str.length() == 0; } /** *

Checks if a String is whitespace, empty ("") or null.

* *
     * StringUtils.isBlank(null)      = true
     * StringUtils.isBlank("")        = true
     * StringUtils.isBlank(" ")       = true
     * StringUtils.isBlank("bob")     = false
     * StringUtils.isBlank("  bob  ") = false
     * 
* * @param str the String to check, may be null * @return true if the String is null, empty or whitespace * @since 2.0 */ public static boolean isBlank(String str) { int strLen; if (str == null || (strLen = str.length()) == 0) { return true; } for (int i = 0; i < strLen; i++) { if ((Character.isWhitespace(str.charAt(i)) == false)) { return false; } } return true; } /** *

Checks if the String contains only whitespace.

* *

null will return false. * An empty String ("") will return true.

* *
     * StringUtils.isWhitespace(null)   = false
     * StringUtils.isWhitespace("")     = true
     * StringUtils.isWhitespace("  ")   = true
     * StringUtils.isWhitespace("abc")  = false
     * StringUtils.isWhitespace("ab2c") = false
     * StringUtils.isWhitespace("ab-c") = false
     * 
* * @param str the String to check, may be null * @return true if only contains whitespace, and is non-null * @since 2.0 */ public static boolean isWhitespace(String str) { if (str == null) { return false; } int sz = str.length(); for (int i = 0; i < sz; i++) { if ((Character.isWhitespace(str.charAt(i)) == false)) { return false; } } return true; } /** * 变成中文括号 * @param str * @return */ public static String bracketToChinese(String str){ if(isBlank(str)){ return str; } String strTrim = str.trim(); strTrim = strTrim.replaceAll("\\(", "(").replaceAll("\\)", ")"); return strTrim; } /** * 变成英文括号 * @param str * @return */ public static String bracketToEnglish(String str){ if(isBlank(str)){ return str; } String strTrim = str.trim(); strTrim = strTrim.replaceAll("(", "(").replaceAll(")", ")"); return strTrim; } /** * 替换字符串 * @param str * @param sourceStr,如果是特殊字符,如英文()、[]等,要使用\\( * @param targetStr * @return */ public static String replaceStr(String str, String sourceStr, String targetStr){ if(isBlank(str)){ return str; } String strTrim = str.trim(); strTrim = strTrim.replaceAll(sourceStr, targetStr); return strTrim; } /** * 根据idsString组装in后面带?的sql * @param idsString String * @return */ public static String getInSql(String idsString){ if(StrUtils.isBlank(idsString)){ throw new RuntimeException("参数不能为空"); } String[] ids = idsString.split(","); StringBuffer sb = new StringBuffer("").append("("); for (String id : ids) { if(!StrUtils.isBlank(id)){ sb.append("?").append(","); } } if(sb.indexOf(",") > -1){ sb.deleteCharAt(sb.length() - 1); } sb.append(")"); return sb.toString(); } /** * 根据idsString字符串,返回组装后的参数params * @param params List * @param idsString String * @return */ public static List setIdsParams(List params, String idsString){ if(StrUtils.isNullList(params) || StrUtils.isBlank(idsString)){ throw new RuntimeException("参数不能为空"); } String[] ids = idsString.split(","); for (String id : ids) { if(!StrUtils.isBlank(id)){ params.add(id); } } return params; } /** * 根据idsString组装in后面带?的sql,并设置params参数值 * @param params List * @param idsString String * @return */ public static String getInSql(List params, String idsString){ if(StrUtils.isNullList(params) || StrUtils.isBlank(idsString)){ throw new RuntimeException("参数不能为空"); } StringBuffer sb = new StringBuffer("").append("("); String[] ids = idsString.split(","); for (String id : ids) { if(!StrUtils.isBlank(id)){ sb.append("?").append(","); params.add(id); } } if(sb.indexOf(",") > -1){ sb.deleteCharAt(sb.length() - 1); } sb.append(")"); return sb.toString(); } /** * 把大写字母转换成_加小写字母(例:Q变为_q) * @param str 必须为一个字符 * @return * @author lqy */ public static String replaceUpperCase(String str){ if(isEmpty(str)){ return str; } return "_" + str.trim().toLowerCase(); } /** * 根据实体名获取tableName,即把大写字母变成_加小写字母 * @param entityName * @return * @author lqy */ public static String clazzNameToTableName(String clazzName){ if(StrUtils.isEmpty(clazzName)){ return null; } String nameTrim = clazzName.trim(); int length = nameTrim.length(); StringBuffer tableName = new StringBuffer(""); if(length > 0){ for (int i = 0; i < length; i++) { char c = nameTrim.charAt(i); if(!"".equals(c)){ if(RegUtils.isUpperCase(String.valueOf(c)) && i > 0 ){//i!=0排除第一个大写字母,不加下划线 tableName.append(StrUtils.replaceUpperCase(String.valueOf(c))); }else{ tableName.append(String.valueOf(c).toLowerCase()); } } } } //System.out.println("=====tableName.toString()="+tableName.toString()); return tableName.toString(); } public static void main(String[] args) throws Exception { /*System.out.println("getUUID = "+getUUID()); System.out.println("getUUID = "+getUUID()); System.out.println("getUUID = "+getUUID()); System.out.println("getUUIDNumberOnly = "+getUUIDNumberOnly());*/ //System.out.println(removeLastCode(" ab ")); } }

 

 

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

©Copyright 蕃薯耀 2017年7月15日

http://fanshuyao.iteye.com/

你可能感兴趣的:(java,蕃薯耀分享)