package com.future.dispatch.utils; import com.google.gson.Gson; import java.io.*; import java.net.*; import java.security.MessageDigest; import java.util.HashMap; import java.util.Map; /** * 融云及时通讯工具类 */ public class RongYunUtil { // @Value("${rongyun.app_key}") private static String app_key = "pwe86ga5p9ar6"; // @Value("${rongyun.app_secret}") private static String app_secret = "FvpwfHqk5o"; /** * 发送系统消息 * * @param fromUserId 发送人id * @param toUserId 接收人id * @param content 消息内容 * @return */ public static boolean pushSystem(String fromUserId, String toUserId, String content) { String url = "http://api-cn.ronghub.com/message/system/publish.json"; Map jsonObject = new HashMap(); jsonObject.put("fromUserId", "1"); jsonObject.put("toUserId", "122"); jsonObject.put("content", "推送消息内容"); String s = pushHttp(url, new Gson().toJson(jsonObject)); jsonObject = new Gson().fromJson(s, Map.class); if (jsonObject.get("code").toString() == "200") { return true; } return false; } /** * 请求接口 * * @param path * @param json * @return */ public static String pushHttp(String path, String json) { String nonce = String.valueOf(Double.valueOf(Math.random() * 1000000.0D).intValue()); String timeMillis = String.valueOf(System.currentTimeMillis()); String signature = getSha1(app_secret + nonce + timeMillis); URL url = null; try { url = new URL(path); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("POST"); connection.setConnectTimeout(1000 * 30); connection.setRequestProperty("Host", "api-cn.ronghub.com"); connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); connection.setRequestProperty("App-Key", app_key); connection.setRequestProperty("Nonce", nonce); connection.setRequestProperty("Timestamp", timeMillis); connection.setRequestProperty("Signature", signature); connection.setDoOutput(true); connection.setDoInput(true); connection.setUseCaches(false); connection.setInstanceFollowRedirects(true); connection.connect(); DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream()); outputStream.writeBytes(json); outputStream.flush(); outputStream.close(); InputStream inputStream = connection.getInputStream(); DataInputStream dataInputStream = new DataInputStream(inputStream); ByteArrayOutputStream stream = new ByteArrayOutputStream(); int len; byte[] bytes = new byte[1024]; while ((len = dataInputStream.read(bytes)) != -1) { stream.write(bytes, 0, len); } return stream.toString(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (ProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } public static String getSha1(String str) { char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; try { MessageDigest mdTemp = MessageDigest.getInstance("SHA1"); mdTemp.update(str.getBytes("UTF-8")); byte[] md = mdTemp.digest(); int j = md.length; char buf[] = new char[j * 2]; int k = 0; for (int i = 0; i < j; i++) { byte byte0 = md[i]; buf[k++] = hexDigits[byte0 >>> 4 & 0xf]; buf[k++] = hexDigits[byte0 & 0xf]; } return new String(buf); } catch (Exception e) { return null; } } }