【Util工具使用】

1、将数组的所有元素使用逗号拼接在一起

/**
	 * 将数组的所有元素使用逗号拼接在一起
	 * @param arr 数组
	 * @return 字符串,例: a,b,c
	 */
	public static String arrayJoin(String[] arr) {
		if(arr == null) {
			return "";
		}
		String str = "";
		for (int i = 0; i < arr.length; i++) {
			str += arr[i];
			if(i != arr.length - 1) {
				str += ",";
			}
		}
		return str;
	}

2、将指定字符串按照逗号分隔符转化为字符串集合

/**
	 * 将指定字符串按照逗号分隔符转化为字符串集合 
	 * @param str 字符串
	 * @return 分割后的字符串集合 
	 */
	public static List<String> convertStringToList(String str) {
		List<String> list = new ArrayList<String>();
		if(isEmpty(str)) {
			return list;
		}
		String[] arr = str.split(",");
		for (String s : arr) {
			s = s.trim();
			if(isEmpty(s) == false) {
				list.add(s);
			}
		}
		return list;
	}

3、将指定集合按照逗号连接成一个字符串

/**
	 * 将指定集合按照逗号连接成一个字符串 
	 * @param list 集合 
	 * @return 字符串 
	 */
	public static String convertListToString(List<?> list) {
		if(list == null || list.size() == 0) {
			return "";
		}
		String str = "";
		for (int i = 0; i < list.size(); i++) {
			str += list.get(i);
			if(i != list.size() - 1) {
				str += ",";
			}
		}
		return str;
	}

4、将日期格式化 (yyyy-MM-dd HH:mm:ss)

/**
	 * 将日期格式化 (yyyy-MM-dd HH:mm:ss)
	 * @param date 日期
	 * @return 格式化后的时间 
	 */
	public static String formatDate(Date date){
		return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
	}

5、String 转 Array,按照逗号切割

    /**
     * String 转 Array,按照逗号切割 
     * @param str 字符串 
     * @return 数组 
     */
    public static String[] convertStringToArray(String str) {
    	List<String> list = convertStringToList(str);
    	return list.toArray(new String[list.size()]);
    }

6、Array 转 String,按照逗号切割

/**
     * Array 转 String,按照逗号切割 
     * @param arr 数组 
     * @return 字符串 
     */
    public static String convertArrayToString(String[] arr) {
    	if(arr == null || arr.length == 0) {
    		return "";
    	}
    	return String.join(",", arr);
    }

7、String数组转集合

  /**
     * String数组转集合 
     * @param strs String数组 
     * @return 集合 
     */
    public static List<String> toList(String... strs) {
    	List<String> list = new ArrayList<>();
    	for (String str : strs) {
    		list.add(str);
		}
    	return list;
    }

8、转化逗号分隔的id到long数组,通常用于批量操作

/**
	 * 转化逗号分隔的id到long数组,通常用于批量操作
	 * @param str
	 * @return
	 */
	public static List<Long> str2longs(String str){
		if(str.length()==0){
			return Collections.EMPTY_LIST;
		}
		String[] array = str.split(",");
		List<Long> rets = new ArrayList(array.length);
		int i = 0;
		for(String id:array){
			try{
				rets.add(Long.parseLong(id));
			}catch(Exception ex){
				throw new RuntimeException("转化 "+str+ " 到Long数组出错");
			}
			
		}
		return rets;
	}

9、向指定 URL 发送POST方法的请求

/**
     * 向指定 URL 发送POST方法的请求
     *
     * @param url    发送请求的 URL
     * @param params 请求的参数集合
     * @return 远程资源的响应结果
     */
    @SuppressWarnings("unused")
    public static String sendPost(String url, Map<String, String> params) {
        OutputStreamWriter out = null;
        BufferedReader in = null;
        StringBuilder result = new StringBuilder();
        try {
            URL realUrl = new URL(url);
            HttpURLConnection conn = (HttpURLConnection) realUrl.openConnection();
            // 发送POST请求必须设置如下两行
            conn.setDoOutput(true);
            conn.setDoInput(true);
            // POST方法
            conn.setRequestMethod("POST");
            // 设置通用的请求属性
            conn.setRequestProperty("accept", "*/*");
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("user-agent",
                    "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            conn.connect();
            // 获取URLConnection对象对应的输出流
            out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
            // 发送请求参数
            if (params != null) {
                StringBuilder param = new StringBuilder();
                for (Map.Entry<String, String> entry : params.entrySet()) {
                    if (param.length() > 0) {
                        param.append("&");
                    }
                    param.append(entry.getKey());
                    param.append("=");
                    param.append(entry.getValue());

                }

                out.write(param.toString());
            }
            // flush输出流的缓冲
            out.flush();
            // 定义BufferedReader输入流来读取URL的响应
            in = new BufferedReader(
                    new InputStreamReader(conn.getInputStream(), "UTF-8"));
            String line;
            while ((line = in.readLine()) != null) {
                result.append(line);
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        //使用finally块来关闭输出流、输入流
        finally {
            try {
                if (out != null) {
                    out.close();
                }
                if (in != null) {
                    in.close();
                }
            } catch (IOException ex) {
                logger.error(ex.getMessage(), ex);
            }
        }
        return result.toString();
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值