Skip to content

Instantly share code, notes, and snippets.

@LuciusChen
Created November 5, 2018 06:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LuciusChen/8ce94782efe13a0f88d193e970145dd8 to your computer and use it in GitHub Desktop.
Save LuciusChen/8ce94782efe13a0f88d193e970145dd8 to your computer and use it in GitHub Desktop.
用于将字节数组转换为字符串
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