miduo_pay/src/main/java/com/jmfy/util/JsonUtil.java

159 lines
5.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 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) throws IOException, ClassNotFoundException {
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(str.getBytes("UTF-8"));
ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
HaoGameParamBean object = (HaoGameParamBean) objectInputStream.readObject();
objectInputStream.close();
byteArrayInputStream.close();
return object;
}
public SortedMap<String, String> getParameterMapYX(HttpServletRequest request) {
SortedMap<String, String> map = new TreeMap<>();
try {
byte[] requestPostBytes = getRequestPostBytes(request);
} catch (IOException e) {
e.printStackTrace();
}
Enumeration paramNames = request.getParameterNames();
while (paramNames.hasMoreElements()) {
String string = (String) paramNames.nextElement();
LOGGER.info("getParameterMapYX param {}", string);
string = string.replace("{", "");
string = string.replace("}", "");
string = string.replace("extra\":", "");
String[] split = string.split(",");
for (String s : split) {
String[] split1 = s.split(":");
String key = split1[0].replace("\"", "");
String value = split1[1].replace("\"", "");
map.put(key, value);
LOGGER.info("getParameterMapYX =================> key={} value={}", key, value);
}
}
return map;
}
public static byte[] getRequestPostBytes(HttpServletRequest request)
throws IOException {
int contentLength = request.getContentLength();
if (contentLength < 0) {
return null;
}
byte buffer[] = new byte[contentLength];
for (int i = 0; i < contentLength; ) {
int readlen = request.getInputStream().read(buffer, i,
contentLength - i);
if (readlen == -1) {
break;
}
i += readlen;
}
return buffer;
}
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;
}
}