yyt
8 天以前 0cceb649e1dc443c2a91d26d81eacb0867c96db3
提交 | 用户 | 时间
0cceb6 1 package com.jcdm.common.utils.http;
Y 2
3 import java.io.BufferedReader;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.io.InputStreamReader;
7 import java.nio.charset.StandardCharsets;
8 import javax.servlet.ServletRequest;
9 import org.apache.commons.lang3.exception.ExceptionUtils;
10 import org.slf4j.Logger;
11 import org.slf4j.LoggerFactory;
12
13 /**
14  * 通用http工具封装
15  * 
16  * @author jc
17  */
18 public class HttpHelper
19 {
20     private static final Logger LOGGER = LoggerFactory.getLogger(HttpHelper.class);
21
22     public static String getBodyString(ServletRequest request)
23     {
24         StringBuilder sb = new StringBuilder();
25         BufferedReader reader = null;
26         try (InputStream inputStream = request.getInputStream())
27         {
28             reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
29             String line = "";
30             while ((line = reader.readLine()) != null)
31             {
32                 sb.append(line);
33             }
34         }
35         catch (IOException e)
36         {
37             LOGGER.warn("getBodyString出现问题!");
38         }
39         finally
40         {
41             if (reader != null)
42             {
43                 try
44                 {
45                     reader.close();
46                 }
47                 catch (IOException e)
48                 {
49                     LOGGER.error(ExceptionUtils.getMessage(e));
50                 }
51             }
52         }
53         return sb.toString();
54     }
55 }