Created
November 5, 2018 06:03
-
-
Save LuciusChen/8ce94782efe13a0f88d193e970145dd8 to your computer and use it in GitHub Desktop.
用于将字节数组转换为字符串
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.InputStreamReader; | |
import java.nio.charset.Charset; | |
import javax.servlet.ServletRequest; | |
/** | |
* 用于将字节数组转换为字符串 | |
* | |
* | |
* | |
*/ | |
public class HttpHelper | |
{ | |
/** | |
* 获取请求Body | |
* | |
* @param request | |
* @return | |
*/ | |
public static String getBodyString(ServletRequest request) | |
{ | |
StringBuilder sb = new StringBuilder(); | |
InputStream inputStream = null; | |
BufferedReader reader = null; | |
try | |
{ | |
inputStream = request.getInputStream(); | |
reader = new BufferedReader( | |
new InputStreamReader(inputStream, Charset.forName("UTF-8"))); | |
String line = ""; | |
while ((line = reader.readLine()) != null) | |
{ | |
sb.append(line); | |
} | |
} | |
catch (IOException e) | |
{ | |
e.printStackTrace(); | |
} | |
finally | |
{ | |
if (inputStream != null) | |
{ | |
try | |
{ | |
inputStream.close(); | |
} | |
catch (IOException e) | |
{ | |
e.printStackTrace(); | |
} | |
} | |
if (reader != null) | |
{ | |
try | |
{ | |
reader.close(); | |
} | |
catch (IOException e) | |
{ | |
e.printStackTrace(); | |
} | |
} | |
} | |
return sb.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment