Skip to content

Instantly share code, notes, and snippets.

@yanhua365
Last active May 24, 2022 03:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save yanhua365/7985602 to your computer and use it in GitHub Desktop.
Save yanhua365/7985602 to your computer and use it in GitHub Desktop.
从HTTP Request里取得body内容得到byte数组的方法。
public byte[] parse(HttpServletRequest request) {
byte[] input = null;
try {
InputStream is = request.getInputStream();
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
while (true) {
int c = is.read();
if (c == -1) break;
byteStream.write((byte) c);
}
if (byteStream.size() > 0){
input = byteStream.toByteArray();
}
} catch (IOException e) {
Throwables.propagate(e);
}
return input;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment