ce2ec2796d6032eee10c3cce9a0695a5c0a1c8a1.svn-base 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. package com.chinacreator.videoalliance.smc.util;
  2. import org.apache.commons.lang.StringUtils;
  3. import java.io.ByteArrayOutputStream;
  4. import java.io.InputStream;
  5. import java.io.OutputStreamWriter;
  6. import java.net.HttpURLConnection;
  7. import java.net.URL;
  8. import java.util.HashMap;
  9. import java.util.Map;
  10. public class URLUtil {
  11. /**
  12. * 解读流
  13. * @param inStream
  14. * @return
  15. * @throws Exception
  16. */
  17. public static String readInputStream(InputStream inStream) throws Exception {
  18. ByteArrayOutputStream outStream = null;
  19. try {
  20. outStream = new ByteArrayOutputStream();
  21. byte[] buffer = new byte[1024];
  22. int len = 0;
  23. while ((len = inStream.read(buffer)) != -1) {
  24. outStream.write(buffer, 0, len);
  25. }
  26. byte[] data = outStream.toByteArray();
  27. return new String(data, "utf-8");
  28. } finally{
  29. if(outStream != null)
  30. outStream.close();
  31. if(inStream != null)
  32. inStream.close();
  33. }
  34. }
  35. /**
  36. * 以get方式获取链接的输出
  37. * @param href
  38. * @return
  39. * @throws Exception
  40. */
  41. public static String get(String href, int timeout) throws Exception {
  42. HttpURLConnection conn = null;
  43. try {
  44. URL url = new URL(href);
  45. conn = (HttpURLConnection) url.openConnection();
  46. conn.setRequestMethod("GET");
  47. if(timeout > 0) {
  48. conn.setConnectTimeout(timeout);
  49. conn.setReadTimeout(timeout);
  50. }
  51. conn.addRequestProperty("content-type", "text/plain");
  52. conn.setUseCaches(false);
  53. conn.setDoOutput(false);
  54. return readInputStream(conn.getInputStream());
  55. } finally {
  56. if(conn != null) {
  57. conn.disconnect();
  58. }
  59. }
  60. }
  61. public static String get(String href) throws Exception {
  62. return get(href, 0);
  63. }
  64. /**
  65. * 以post方式获取链接的输出
  66. * @param href
  67. * @return
  68. * @throws Exception
  69. */
  70. public static String post(String href, String data, int timeout, String contentType) throws Exception {
  71. HttpURLConnection conn = null;
  72. OutputStreamWriter out = null;
  73. if(data == null || data.length() == 0) {
  74. return get(href, timeout);
  75. } else {
  76. try {
  77. URL url = new URL(href);
  78. conn = (HttpURLConnection) url.openConnection();
  79. conn.setRequestMethod("POST");
  80. if(timeout > 0) {
  81. conn.setConnectTimeout(timeout);
  82. conn.setReadTimeout(timeout);
  83. }
  84. conn.addRequestProperty("content-type", contentType);
  85. conn.setDoOutput(true);
  86. conn.setUseCaches(false);
  87. out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
  88. out.write(data);
  89. out.flush();
  90. } finally {
  91. if(out != null) {
  92. out.close();
  93. }
  94. if(conn != null) {
  95. conn.disconnect();
  96. }
  97. }
  98. if(conn != null) {
  99. return readInputStream(conn.getInputStream());
  100. }
  101. return null;
  102. }
  103. }
  104. public static String post(String href, String data, int timeout) throws Exception {
  105. return post(href, data, timeout, "text/plain");
  106. }
  107. public static String post(String href, String data) throws Exception {
  108. return post(href, data, 0);
  109. }
  110. public static String postJson(String href, String data) throws Exception {
  111. return post(href, data, 0, "application/json;charset=utf-8");
  112. }
  113. public static String postForm(String href, String data, int timeout) throws Exception {
  114. return post(href, data, timeout, "application/x-www-form-urlencoded");
  115. }
  116. public static String postForm(String href, String data) throws Exception {
  117. return postForm(href, data, 0);
  118. }
  119. /**
  120. * 把map转换为url地址
  121. * @param server 项目地址
  122. * @param map 存储参数的map
  123. * @return 返回完整的url地址
  124. */
  125. public static String mapToUrl(String server, Map<String,String> map){
  126. StringBuilder url = new StringBuilder(server);
  127. StringBuilder param = new StringBuilder("?");
  128. if (map == null) {
  129. return url.toString();
  130. }
  131. for(Map.Entry<String,String> entry: map.entrySet()){
  132. String key = entry.getKey();
  133. String val = entry.getValue();
  134. if(key.equalsIgnoreCase("action")){
  135. url.append(val).append(".do");
  136. } else {
  137. param.append(key).append("=").append(val).append("&");
  138. }
  139. }
  140. //删去最后的&
  141. if (param.toString().endsWith("&")) {
  142. param.deleteCharAt(param.length() - 1);
  143. }
  144. url.append(param);
  145. return url.toString();
  146. }
  147. /**
  148. * 把url的参数转换成Map
  149. * @param param 参数串
  150. * @return 返回存储Map
  151. */
  152. public static Map<String, String> urlParamsToMap(String param) {
  153. Map<String, String> map = new HashMap<String, String>(0);
  154. if (StringUtils.isBlank(param)) {
  155. return map;
  156. }
  157. String[] params = param.split("&");
  158. for (int i = 0; i < params.length; i++) {
  159. String[] p = params[i].split("=");
  160. if (p.length == 2) {
  161. map.put(p[0], p[1]);
  162. }
  163. }
  164. return map;
  165. }
  166. public static void main(String[] args) throws Exception {
  167. String url = "http://114.255.201.228:86/mobile-report/sendAlarmMsg.do";
  168. String result = postForm(url, "name=CDN_BUSINESS&content=测试短信");
  169. System.out.println(result);
  170. }
  171. }