Forked from yanhua365/ParseRequestBodyToBytes.java
Created
November 12, 2015 07:18
-
-
Save user20161119/c2dfe9e4738fa32ba155 to your computer and use it in GitHub Desktop.
从HTTP Request里取得body内容得到byte数组的方法。
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
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