Browse Source

项目合并

suntg 7 years ago
parent
commit
3406ca671a

+ 57 - 7
src/main/java/com/uas/platform/b2b/support/HttpUtils.java

@@ -32,8 +32,24 @@ import java.net.URLEncoder;
 import java.util.*;
 import java.util.Map.Entry;
 
+/**
+ * HTTP请求
+ *
+ * @author hejq
+ * @date 2018-06-28 18:00
+ */
 public class HttpUtils {
 
+	/**
+	 * ?标签
+	 */
+	private static String QUOTATION_MARK = "?";
+
+    /**
+     * &标签
+     */
+	private static String AND_MARK = "&";
+
 	/**
 	 * 发起POST、PUT请求
 	 * 
@@ -48,10 +64,11 @@ public class HttpUtils {
 		try {
 			StringBuilder buf = new StringBuilder(url);
 			if (params != null && !params.isEmpty()) {
-				if (url.indexOf("?") == -1)
-					buf.append("?");
-				else if (!url.endsWith("&"))
-					buf.append("&");
+				if (url.indexOf(QUOTATION_MARK) == -1) {
+                    buf.append(QUOTATION_MARK);
+                } else if (!url.endsWith(AND_MARK)) {
+                    buf.append(AND_MARK);
+                }
 				Set<Entry<String, Object>> entrys = params.entrySet();
 				for (Entry<String, Object> entry : entrys) {
 					buf.append(entry.getKey()).append("=").append(URLEncoder.encode(String.valueOf(entry.getValue()), "UTF-8")).append("&");
@@ -107,6 +124,38 @@ public class HttpUtils {
 		}
 	}
 
+	/**
+	 * 发起POST、PUT请求
+	 *
+	 * @param datas
+	 * @return
+	 * @throws Exception
+	 */
+	public static Response doPost(String url, Collection<?> datas) throws Exception {
+		CloseableHttpClient httpClient = HttpClients.createDefault();
+		HttpEntityEnclosingRequestBase request = new HttpPost(url);
+		CloseableHttpResponse response = null;
+		try {
+			if (datas != null && !datas.isEmpty()) {
+				request.setEntity(new StringEntity(FlexJsonUtils.toJson(datas), ContentType.create("application/json", Consts.UTF_8)));
+			}
+			response = httpClient.execute(request);
+			return Response.getResponse(response);
+		} finally {
+			request.releaseConnection();
+			try {
+				httpClient.close();
+			} catch (IOException e) {
+			}
+			if (response != null) {
+				try {
+					response.close();
+				} catch (IOException e) {
+				}
+			}
+		}
+	}
+
 	/**
 	 * 发起POST、PUT请求
 	 *
@@ -285,7 +334,7 @@ public class HttpUtils {
             httpPost.setEntity(reqEntity);
             response = httpClient.execute(httpPost);
             Response res = Response.getResponse(response);
-            if(res.getStatusCode() == 200) {
+            if (res.getStatusCode() == HttpStatus.SC_OK) {
                 JSONObject obj = JSONObject.parseObject(res.getResponseText());
                 path = (String) obj.get("path");
             }
@@ -352,8 +401,9 @@ public class HttpUtils {
 		}
 
 		public static Response getResponse(HttpResponse response) throws IllegalStateException, IOException, Exception {
-			if (response != null)
-				return new Response(response);
+			if (response != null) {
+                return new Response(response);
+            }
 			return null;
 		}
 	}

+ 2 - 1
src/main/webapp/WEB-INF/web.xml

@@ -8,7 +8,7 @@
 	<description>usoftchina platform b2b</description>
 	<context-param>
 		<param-name>webAppRootKey</param-name>
-		<param-value>spring1.webapp.root_scm</param-value>
+		<param-value>spring.webapp.root_b2b</param-value>
 	</context-param>
 	<context-param>
 		<param-name>spring.profiles.active</param-name>
@@ -130,6 +130,7 @@
 		<filter-name>cors</filter-name>
 		<url-pattern>/*</url-pattern>
 	</filter-mapping>
+
 	<error-page>
 		<error-code>404</error-code>
 		<location>/static/tpl/error/index.html</location>