package processing; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import entity.Solution; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; import javax.servlet.http.HttpServletRequest; import java.io.BufferedReader; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.*; public class MyFunc { /** * 将后台返回的数据转成json格式 */ public static String toJson(Solution ans) { JSONObject jobj = new JSONObject(); jobj.put("solution", ans.getAnswer()); jobj.put("status", ans.getStatus()); jobj.put("process", ans.getNext()); return jobj.toString(); } public static String toJson(String returnStr) { JSONObject jobj = new JSONObject(); jobj.put("returnString", returnStr); return jobj.toString(); } /** * 当请求中的数据是json格式时,该函数用于提取request中的json数据 * * @param request * @return json字符串 */ public static String readJSONString(HttpServletRequest request) { StringBuffer json = new StringBuffer(); String line = null; try { BufferedReader reader = request.getReader(); while ((line = reader.readLine()) != null) { json.append(line); } } catch (IOException e) { e.printStackTrace(); } return json.toString(); } /** * 通过get方法传递参数时需要对参数的编码进行转换 */ public static String convert2Utf8(String s) { if (s == null) return null; try { s = new String(s.getBytes("iso-8859-1"), "utf-8"); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } return s; } public static long transferToMillisecond(String keyword) throws ParseException { long time; System.out.println(keyword); String timeSeries[] = {"时", "分", "秒"}; for (String f : timeSeries) { if (!keyword.contains(f)) keyword += "0" + f; } SimpleDateFormat format = new SimpleDateFormat("yyyy年M月d日H时m分s秒"); time = format.parse(keyword).getTime(); return time; } public static String transferToStandardTime(long time) throws ParseException { SimpleDateFormat format = new SimpleDateFormat("yyyy年M月d日H时m分s秒"); Date date = new Date(time); return format.format(date); } public static boolean isInteger(String str){ return true; } public static JSONObject postJSON(String url, JSONObject data) throws UnsupportedEncodingException { JSONObject jobj = new JSONObject(); HttpPost httpPost = new HttpPost(url); CloseableHttpClient client = HttpClients.createDefault(); String respContent = null; String keyName = null; StringEntity entity = new StringEntity(data.toJSONString(), "utf-8"); entity.setContentType("application/json; charset=utf-8"); httpPost.setEntity(entity); try { HttpResponse resp = client.execute(httpPost); if (resp.getStatusLine().getStatusCode() == 200) { HttpEntity he = resp.getEntity(); respContent = EntityUtils.toString(he, "UTF-8"); jobj = JSONObject.parseObject(respContent); } else { jobj.put("status", 0); } } catch (Exception e) { e.printStackTrace(); } return jobj; } }