From 951bef16f21f39139ca88cf1dd0e7e349f11a8fa Mon Sep 17 00:00:00 2001
From: maliang
Date: Fri, 3 Mar 2017 17:48:12 +0800
Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=90=8D=E7=A7=B0?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../src/main/java/com/lyms/util/HttpUtils.java | 87 ++++++++++------------
1 file changed, 39 insertions(+), 48 deletions(-)
diff --git a/core.sdk/src/main/java/com/lyms/util/HttpUtils.java b/core.sdk/src/main/java/com/lyms/util/HttpUtils.java
index 9fa1c95..9e23876 100644
--- a/core.sdk/src/main/java/com/lyms/util/HttpUtils.java
+++ b/core.sdk/src/main/java/com/lyms/util/HttpUtils.java
@@ -33,7 +33,6 @@ import org.slf4j.LoggerFactory;
import com.lyms.util.remote.HttpRemote;
-
/**
*
* HTTP工具类
@@ -43,17 +42,14 @@ import com.lyms.util.remote.HttpRemote;
public class HttpUtils {
private static final Logger logger = LoggerFactory.getLogger(HttpUtils.class);
-
-
+
private static final String ENCODING = "UTF-8";
-
-
+
/**
* http远程调用
*/
- public static HttpRemote REMOTE = HttpRemote.build();
-
-
+ public static final HttpRemote REMOTE = HttpRemote.build();
+
/**
*
* 允许 JS 跨域设置
@@ -61,23 +57,19 @@ public class HttpUtils {
*
*
*
- * http {
- * ......
- * add_header Access-Control-Allow-Origin *;
- * ......
- * }
+ * http { ...... add_header Access-Control-Allow-Origin *; ...... }
*
*
*
- * 非 ngnix 下,如果该方法设置不管用、可以尝试增加下行代码。
+ * 非 ngnix 下,如果该方法设置不管用、可以尝试增加下行代码。
*
* response.setHeader("Access-Control-Allow-Origin", "*");
*
*
* @param response
- * 响应请求
+ * 响应请求
*/
- public static void allowJsCrossDomain( HttpServletResponse response ) {
+ public static void allowJsCrossDomain(HttpServletResponse response) {
response.setHeader("Access-Control-Allow-Credentials", "true");
response.setHeader("Access-Control-Allow-Methods", "GET, OPTIONS, POST, PUT, DELETE");
response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
@@ -91,13 +83,13 @@ public class HttpUtils {
*
*
* @param request
- * 当前请求
+ * 当前请求
* @return
*/
- public static boolean isAjax( HttpServletRequest request ) {
+ public static boolean isAjax(HttpServletRequest request) {
return "XMLHttpRequest".equals(request.getHeader("X-Requested-With")) ? true : false;
}
-
+
/**
*
*
@@ -106,17 +98,17 @@ public class HttpUtils {
*
* @param response
* @param status
- * HTTP 状态码
+ * HTTP 状态码
* @param tip
*/
- public static void ajaxStatus( HttpServletResponse response, int status, String tip ) {
+ public static void ajaxStatus(HttpServletResponse response, int status, String tip) {
try {
response.setContentType("text/html;charset=" + ENCODING);
response.setStatus(status);
PrintWriter out = response.getWriter();
out.print(tip);
out.flush();
- } catch ( IOException e ) {
+ } catch (IOException e) {
logger.error(e.toString());
e.printStackTrace();
}
@@ -216,7 +208,7 @@ public class HttpUtils {
logger.error("encodeRetURL error." + url);
e.printStackTrace();
}
-
+
if (data != null) {
for (Map.Entry entry : data.entrySet()) {
retStr.append("&").append(entry.getKey()).append("=").append(entry.getValue());
@@ -225,7 +217,7 @@ public class HttpUtils {
return retStr.toString();
}
-
+
/**
*
* URLDecoder 解码地址
@@ -240,7 +232,7 @@ public class HttpUtils {
return null;
}
String retUrl = "";
-
+
try {
retUrl = URLDecoder.decode(url, ENCODING);
} catch (UnsupportedEncodingException e) {
@@ -280,7 +272,7 @@ public class HttpUtils {
}
return false;
}
-
+
/**
*
*
@@ -288,9 +280,9 @@ public class HttpUtils {
*
*
* @param response
- * 请求响应
+ * 请求响应
* @param location
- * 重定向至地址
+ * 重定向至地址
*/
public static void sendRedirect(HttpServletResponse response, String location) {
try {
@@ -358,9 +350,8 @@ public class HttpUtils {
}
return url.toString();
}
-
-
- /**
+
+ /**
* @Description:输出类型:json
* @param data
* @param response
@@ -368,25 +359,26 @@ public class HttpUtils {
* 创建时间:2016年9月20日
* 修改人:
* 修改时间:
- */
- public static void writeJsonText(String data,HttpServletResponse response){
- PrintWriter pw = null;
+ */
+ public static void writeJsonText(String data, HttpServletResponse response) {
+ PrintWriter pw = null;
try {
- response.setCharacterEncoding("UTF-8");
- response.setContentType("application/json; charset=utf-8");
+ response.setCharacterEncoding("UTF-8");
+ response.setContentType("application/json; charset=utf-8");
pw = response.getWriter();
pw.write(data);
pw.flush();
} catch (IOException e) {
e.printStackTrace();
- }finally {
- if(pw != null){
+ } finally {
+ if (pw != null) {
pw.flush();
pw.close();
}
}
}
- /**
+
+ /**
* @Description:输出类型:text/html
* @param data
* @param response
@@ -394,24 +386,23 @@ public class HttpUtils {
* 创建时间:2016年9月20日
* 修改人:
* 修改时间:
- */
- public static void writeText(String data,HttpServletResponse response){
- PrintWriter pw = null;
+ */
+ public static void writeText(String data, HttpServletResponse response) {
+ PrintWriter pw = null;
try {
- response.setCharacterEncoding("UTF-8");
- response.setContentType("text/html; charset=utf-8");
+ response.setCharacterEncoding("UTF-8");
+ response.setContentType("text/html; charset=utf-8");
pw = response.getWriter();
pw.write(data);
pw.flush();
} catch (IOException e) {
e.printStackTrace();
- }finally {
- if(pw != null){
+ } finally {
+ if (pw != null) {
pw.flush();
pw.close();
}
}
}
-
-
+
}
--
1.8.3.1