admin
2024-10-15 6d313a4a6f9f9d7ea65ec50ab0d4491e595edb97
提交 | 用户 | 时间
e57a89 1 package com.jcdm.common.utils.http;
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 }