微信公众号支付H5调用支付解析(5)

public final class Tools { public static final String inputStream2String(InputStream in) throws UnsupportedEncodingException, IOException{ if(in == null) return ""; StringBuffer out = new StringBuffer(); byte[] b = new byte[4096]; for (int n; (n = in.read(b)) != -1;) { out.append(new String(b, 0, n, "UTF-8")); } return out.toString(); } public static final boolean checkSignature(String token,String signature,String timestamp,String nonce){ List<String> params = new ArrayList<String>(); params.add(token); params.add(timestamp); params.add(nonce); Collections.sort(params,new Comparator<String>() { @Override public int compare(String o1, String o2) { return o1.compareTo(o2); } }); String temp = params.get(0)+params.get(1)+params.get(2); return SHA1.encode(temp).equals(signature); } }

相应前端数据工具WebUtil.java工具类

public class WebUtil { public static Object getSessionAttribute(HttpServletRequest req, String key) { Object ret = null; try { ret = req.getSession(false).getAttribute(key); } catch (Exception e) { } return ret; } public static void response(HttpServletResponse response, String result) { try { response.setContentType("application/json;charset=utf-8"); response.getWriter().write(result); } catch (IOException e) { e.printStackTrace(); } } public static void response(HttpServletResponse response, ResponseMessage result) { try { response.setContentType("application/json;charset=utf-8"); response.getWriter().write(JsonUtil.objectToJsonNode(result).toString()); } catch (Exception e) { e.printStackTrace(); } } public static String packJsonp(String callback, String json) { if (json == null) { json = ""; } if (callback == null || callback.isEmpty()) { return json; } return callback + "&&" + callback + '(' + json + ')'; } public static String packJsonp(String callback, ResponseMessage response) { String json = null; if (response == null) { json = ""; } else { json = JsonUtil.objectToJsonNode(response).toString(); } if (callback == null || callback.isEmpty()) { return json; } return callback + "&&" + callback + '(' + json + ')'; } }

Json转换工具JsonUtil.java

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wzgzzg.html