package com.iamberry.wechat.tools;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URLDecoder;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

/**
 * 字符串工具类
 * @author 穆再兴
 * @author Administrator
 *
 */
public class StrUtils {
	public static String checkStr(Object obj){
		if(obj == null){
			return "";
		}
		return obj.toString();
	}
	
	/**
	 * 用流的方式获取post请求的参数字符串
	 * create date 2016年6月1日
	 * @author 穆再兴
	 * @param req
	 * @return
	 */
	public static String getPostParamsStr(HttpServletRequest req){
		BufferedReader br = null;
        String line = null;
        StringBuffer sb = new StringBuffer();
        String postParams = "";
		try {
			req.setCharacterEncoding("UTF-8");
			br = new BufferedReader(new InputStreamReader(req.getInputStream()));
			while ((line = br.readLine()) != null) {
				line = URLDecoder.decode(line,"UTF-8");
				sb.append(line);
			}
			postParams = sb.toString();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return postParams;
	}
	
	/**
	 * 获取 uuid字符串<32 位 >
	 * @return
	 */
	public static synchronized String getUUID(){
	    UUID uuid=UUID.randomUUID();
	    String str = uuid.toString(); 
	    String uuidStr=str.replace("-", "");
	    return uuidStr;
	}
}
