generated from root/miduo_server
104 lines
3.3 KiB
Java
104 lines
3.3 KiB
Java
package com.jmfy.util;
|
|
|
|
import com.google.gson.Gson;
|
|
import com.jmfy.paramBean.HaoGameParamBean;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import java.io.ByteArrayInputStream;
|
|
import java.io.IOException;
|
|
import java.io.ObjectInputStream;
|
|
import java.text.ParseException;
|
|
import java.text.SimpleDateFormat;
|
|
import java.util.*;
|
|
|
|
public class JsonUtil {
|
|
|
|
private static final Logger LOGGER = LoggerFactory.getLogger(JsonUtil.class);
|
|
private static JsonUtil jsonUtil;
|
|
|
|
private static Gson gson = new Gson();
|
|
|
|
private static class InnerClass {
|
|
private static final JsonUtil SINGLETON = new JsonUtil();
|
|
}
|
|
|
|
public static JsonUtil getInstence() {
|
|
if (jsonUtil == null) {
|
|
jsonUtil = JsonUtil.InnerClass.SINGLETON;
|
|
}
|
|
return jsonUtil;
|
|
}
|
|
|
|
public Gson getGson() {
|
|
return gson;
|
|
}
|
|
|
|
public HashMap<String, String> getParameterMap2(HttpServletRequest request) throws IOException {
|
|
HashMap<String, String> map = new HashMap();
|
|
Enumeration paramNames = request.getParameterNames();
|
|
while (paramNames.hasMoreElements()) {
|
|
String paramName = (String) paramNames.nextElement();
|
|
String[] paramValues = request.getParameterValues(paramName);
|
|
String paramValue = paramValues[0];
|
|
map.put(paramName, paramValue);
|
|
LOGGER.info("parameter :" + paramName + "= {} ", paramValue);
|
|
}
|
|
return map;
|
|
}
|
|
|
|
public HashMap<String, String> getParameterMap(HttpServletRequest request) throws IOException {
|
|
HashMap<String, String> map = new HashMap();
|
|
Enumeration paramNames = request.getParameterNames();
|
|
while (paramNames.hasMoreElements()) {
|
|
String paramName = (String) paramNames.nextElement();
|
|
String[] paramValues = request.getParameterValues(paramName);
|
|
String paramValue = paramValues[0];
|
|
String key = paramName;
|
|
if (!StringUtils.checkIsEmpty(paramValue)) {
|
|
key = paramName + "=" + paramValue;
|
|
}
|
|
map.put(key, paramValue);
|
|
LOGGER.info("parameter : {} ", key);
|
|
}
|
|
return map;
|
|
}
|
|
|
|
|
|
public static HaoGameParamBean serializeToObject(String str) {
|
|
HaoGameParamBean haoGameParamBean = gson.fromJson(str, HaoGameParamBean.class);
|
|
return haoGameParamBean;
|
|
}
|
|
|
|
public static String getServiceKey(String serviceName, String host, String port) {
|
|
StringBuilder sb = new StringBuilder();
|
|
return sb.append(serviceName).append("|")
|
|
.append(host).append("|")
|
|
.append(port).toString();
|
|
}
|
|
|
|
/**
|
|
* 获取日期前一天
|
|
*
|
|
* @param specifiedDay
|
|
* @return
|
|
*/
|
|
public static String getSpecifiedDayBefore(String specifiedDay) {
|
|
//SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
|
|
Calendar c = Calendar.getInstance();
|
|
Date date = null;
|
|
try {
|
|
date = new SimpleDateFormat("yy-MM-dd").parse(specifiedDay);
|
|
} catch (ParseException e) {
|
|
e.printStackTrace();
|
|
}
|
|
c.setTime(date);
|
|
int day = c.get(Calendar.DATE);
|
|
c.set(Calendar.DATE, day - 1);
|
|
|
|
String dayBefore = new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
|
|
return dayBefore;
|
|
}
|
|
}
|