实体类 转 Map
public static Map<String, Object> convertBeanToMap(Object obj) throws IntrospectionException {
if (obj == null) {
return null;
}
Map<String, Object> map = new HashMap<String, Object>();
try {
BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor property : propertyDescriptors) {
String key = property.getName();
// 过滤class属性
if (!key.equals("class")) {
// 得到property对应的getter方法
Method getter = property.getReadMethod();
Object value = getter.invoke(obj);
if (null == value) {
map.put(key, "");
} else {
map.put(key, value);
}
}
}
} catch (Exception e) {
}
return map;
}
根据url获取InpuStream
public InputStream getFileByUrl(String urlStr) {
try {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet method = new HttpGet(urlStr);
HttpResponse result = httpClient.execute(method);
InputStream inputStream = result.getEntity().getContent();
return inputStream;
} catch (Exception e) {
e.printStackTrace();
log.error("======通过url获取 InputStream 异常===",e);
}
return null;
}
InputStream
body传值
public static String postBody(String urlStr,Map<String, Object> paramMap){
OutputStreamWriter out = null;
BufferedReader in = null;
StringBuilder result = new StringBuilder();
HttpURLConnection conn = null;
try{
URL url = new URL(urlStr);
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
//发送POST请求必须设置为true
conn.setDoOutput(true);
conn.setDoInput(true);
//设置连接超时时间和读取超时时间
conn.setConnectTimeout(30000);
conn.setReadTimeout(10000);
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("Accept", "application/json");
//获取输出流
out = new OutputStreamWriter(conn.getOutputStream());
out.write(JSONObject.toJSONString(paramMap));
out.flush();
out.close();
//取得输入流,并使用Reader读取
if (200 == conn.getResponseCode()){
in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
String line;
while ((line = in.readLine()) != null){
result.append(line);
}
}else{
System.out.println("ResponseCode is an error code:" + conn.getResponseCode());
}
}catch (Exception e){
e.printStackTrace();
}finally {
try{
if(out != null){
out.close();
}
if(in != null){
in.close();
}
}catch (IOException ioe){
ioe.printStackTrace();
}
}
return result.toString();
}
params传值
public static String post(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) {
e.printStackTrace();
}
//使用finally块来关闭输出流、输入流
finally{
try{
if(out!=null){
out.close();
}
if(in!=null){
in.close();
}
}
catch(IOException ex){
ex.printStackTrace();
}
}
return result.toString();
}
身份证常用方法
年级
public static int IdNOToAge(String IdNO) {
int leh = IdNO.length();
String dates = "";
if (leh == 18) {
dates = IdNO.substring(6, 10);
SimpleDateFormat df = new SimpleDateFormat("yyyy");
String year = df.format(new Date());
int u = Integer.parseInt(year) - Integer.parseInt(dates);
return u;
} else {
dates = IdNO.substring(6, 8);
return Integer.parseInt(dates);
}
出生年月
public static String getBirthday(String IDCard) {
String year = IDCard.substring(6, 10);
String month = IDCard.substring(10,12);
String day = IDCard.substring(12,14);
return year + "-" + month + "-" + day;
}
性别
public static String getGender(String IDCard) {
String sex = "";
if (StringUtils.isNotBlank(IDCard)) {
//15位身份证号
if (IDCard.length() == 15) {
if (Integer.parseInt(IDCard.substring(14, 15)) % 2 == 0) {
sex = "1";
} else {
sex = "0";
}
//18位身份证号
} else if (IDCard.length() == 18) {
// 判断性别
if (Integer.parseInt(IDCard.substring(16).substring(0, 1)) % 2 == 0) {
sex = "1";
} else {
sex = "0";
}
}
}
return sex;
}