generated from root/miduo_server
141 lines
4.7 KiB
Java
141 lines
4.7 KiB
Java
package com.jmfy.util;
|
|
|
|
import com.google.gson.Gson;
|
|
import com.jmfy.controller.RechargeController;
|
|
import com.jmfy.redisProperties.RedisUserKey;
|
|
import org.json.JSONObject;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import java.io.BufferedReader;
|
|
import java.io.IOException;
|
|
import java.net.InetAddress;
|
|
import java.net.UnknownHostException;
|
|
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 HashMap<String, String> getParameterMapYX(HttpServletRequest request) {
|
|
HashMap<String, String> map = new HashMap();
|
|
Enumeration paramNames = request.getParameterNames();
|
|
while (paramNames.hasMoreElements()) {
|
|
String paramName = (String) paramNames.nextElement();
|
|
JSONObject data = new JSONObject(paramName);
|
|
Set<String> strings = data.keySet();
|
|
for (String string : strings) {
|
|
string = string.replace("{", "");
|
|
string = string.replace("}", "");
|
|
string = string.replace("extra\":", "");
|
|
String[] split = string.split(",");
|
|
for (String s : split) {
|
|
String[] split1 = s.split(":");
|
|
map.put(split1[0].replace("\"", ""), split1[1].replace("\"", ""));
|
|
}
|
|
}
|
|
}
|
|
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;
|
|
}
|
|
}
|