Commit 951bef16f21f39139ca88cf1dd0e7e349f11a8fa

Authored by maliang
1 parent cd163d2240
Exists in master

修改名称

Showing 1 changed file with 39 additions and 48 deletions

core.sdk/src/main/java/com/lyms/util/HttpUtils.java View file @ 951bef1
... ... @@ -33,7 +33,6 @@
33 33  
34 34 import com.lyms.util.remote.HttpRemote;
35 35  
36   -
37 36 /**
38 37 * <p>
39 38 * HTTP工具类
40 39  
41 40  
... ... @@ -43,17 +42,14 @@
43 42 public class HttpUtils {
44 43  
45 44 private static final Logger logger = LoggerFactory.getLogger(HttpUtils.class);
46   -
47   -
  45 +
48 46 private static final String ENCODING = "UTF-8";
49   -
50   -
  47 +
51 48 /**
52 49 * http远程调用
53 50 */
54   - public static HttpRemote REMOTE = HttpRemote.build();
55   -
56   -
  51 + public static final HttpRemote REMOTE = HttpRemote.build();
  52 +
57 53 /**
58 54 *
59 55 * 允许 JS 跨域设置
60 56  
61 57  
62 58  
... ... @@ -61,23 +57,19 @@
61 57 * <p>
62 58 * <!-- 使用 nginx 注意在 nginx.conf 中配置 -->
63 59 *
64   - * http {
65   - * ......
66   - * add_header Access-Control-Allow-Origin *;
67   - * ......
68   - * }
  60 + * http { ...... add_header Access-Control-Allow-Origin *; ...... }
69 61 * </p>
70 62 *
71 63 * <p>
72   - * 非 ngnix 下,如果该方法设置不管用、可以尝试增加下行代码。
  64 + * 非 ngnix 下,如果该方法设置不管用、可以尝试增加下行代码。
73 65 *
74 66 * response.setHeader("Access-Control-Allow-Origin", "*");
75 67 * </p>
76 68 *
77 69 * @param response
78   - * 响应请求
  70 + * 响应请求
79 71 */
80   - public static void allowJsCrossDomain( HttpServletResponse response ) {
  72 + public static void allowJsCrossDomain(HttpServletResponse response) {
81 73 response.setHeader("Access-Control-Allow-Credentials", "true");
82 74 response.setHeader("Access-Control-Allow-Methods", "GET, OPTIONS, POST, PUT, DELETE");
83 75 response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
84 76  
85 77  
... ... @@ -91,13 +83,13 @@
91 83 * </p>
92 84 *
93 85 * @param request
94   - * 当前请求
  86 + * 当前请求
95 87 * @return
96 88 */
97   - public static boolean isAjax( HttpServletRequest request ) {
  89 + public static boolean isAjax(HttpServletRequest request) {
98 90 return "XMLHttpRequest".equals(request.getHeader("X-Requested-With")) ? true : false;
99 91 }
100   -
  92 +
101 93 /**
102 94 *
103 95 * <p>
104 96  
105 97  
... ... @@ -106,17 +98,17 @@
106 98 *
107 99 * @param response
108 100 * @param status
109   - * HTTP 状态码
  101 + * HTTP 状态码
110 102 * @param tip
111 103 */
112   - public static void ajaxStatus( HttpServletResponse response, int status, String tip ) {
  104 + public static void ajaxStatus(HttpServletResponse response, int status, String tip) {
113 105 try {
114 106 response.setContentType("text/html;charset=" + ENCODING);
115 107 response.setStatus(status);
116 108 PrintWriter out = response.getWriter();
117 109 out.print(tip);
118 110 out.flush();
119   - } catch ( IOException e ) {
  111 + } catch (IOException e) {
120 112 logger.error(e.toString());
121 113 e.printStackTrace();
122 114 }
... ... @@ -216,7 +208,7 @@
216 208 logger.error("encodeRetURL error." + url);
217 209 e.printStackTrace();
218 210 }
219   -
  211 +
220 212 if (data != null) {
221 213 for (Map.Entry<String, String> entry : data.entrySet()) {
222 214 retStr.append("&").append(entry.getKey()).append("=").append(entry.getValue());
... ... @@ -225,7 +217,7 @@
225 217  
226 218 return retStr.toString();
227 219 }
228   -
  220 +
229 221 /**
230 222 * <p>
231 223 * URLDecoder 解码地址
... ... @@ -240,7 +232,7 @@
240 232 return null;
241 233 }
242 234 String retUrl = "";
243   -
  235 +
244 236 try {
245 237 retUrl = URLDecoder.decode(url, ENCODING);
246 238 } catch (UnsupportedEncodingException e) {
... ... @@ -280,7 +272,7 @@
280 272 }
281 273 return false;
282 274 }
283   -
  275 +
284 276 /**
285 277 *
286 278 * <p>
287 279  
... ... @@ -288,9 +280,9 @@
288 280 * </p>
289 281 *
290 282 * @param response
291   - * 请求响应
  283 + * 请求响应
292 284 * @param location
293   - * 重定向至地址
  285 + * 重定向至地址
294 286 */
295 287 public static void sendRedirect(HttpServletResponse response, String location) {
296 288 try {
... ... @@ -358,9 +350,8 @@
358 350 }
359 351 return url.toString();
360 352 }
361   -
362   -
363   - /**
  353 +
  354 + /**
364 355 * <li>@Description:输出类型:json
365 356 * <li>@param data
366 357 * <li>@param response
367 358  
368 359  
369 360  
... ... @@ -368,25 +359,26 @@
368 359 * <li>创建时间:2016年9月20日
369 360 * <li>修改人:
370 361 * <li>修改时间:
371   - */
372   - public static void writeJsonText(String data,HttpServletResponse response){
373   - PrintWriter pw = null;
  362 + */
  363 + public static void writeJsonText(String data, HttpServletResponse response) {
  364 + PrintWriter pw = null;
374 365 try {
375   - response.setCharacterEncoding("UTF-8");
376   - response.setContentType("application/json; charset=utf-8");
  366 + response.setCharacterEncoding("UTF-8");
  367 + response.setContentType("application/json; charset=utf-8");
377 368 pw = response.getWriter();
378 369 pw.write(data);
379 370 pw.flush();
380 371 } catch (IOException e) {
381 372 e.printStackTrace();
382   - }finally {
383   - if(pw != null){
  373 + } finally {
  374 + if (pw != null) {
384 375 pw.flush();
385 376 pw.close();
386 377 }
387 378 }
388 379 }
389   - /**
  380 +
  381 + /**
390 382 * <li>@Description:输出类型:text/html
391 383 * <li>@param data
392 384 * <li>@param response
393 385  
394 386  
395 387  
... ... @@ -394,25 +386,24 @@
394 386 * <li>创建时间:2016年9月20日
395 387 * <li>修改人:
396 388 * <li>修改时间:
397   - */
398   - public static void writeText(String data,HttpServletResponse response){
399   - PrintWriter pw = null;
  389 + */
  390 + public static void writeText(String data, HttpServletResponse response) {
  391 + PrintWriter pw = null;
400 392 try {
401   - response.setCharacterEncoding("UTF-8");
402   - response.setContentType("text/html; charset=utf-8");
  393 + response.setCharacterEncoding("UTF-8");
  394 + response.setContentType("text/html; charset=utf-8");
403 395 pw = response.getWriter();
404 396 pw.write(data);
405 397 pw.flush();
406 398 } catch (IOException e) {
407 399 e.printStackTrace();
408   - }finally {
409   - if(pw != null){
  400 + } finally {
  401 + if (pw != null) {
410 402 pw.flush();
411 403 pw.close();
412 404 }
413 405 }
414 406 }
415   -
416   -
  407 +
417 408 }