我想像我的网络服务一样发送图像byte[]
。发送请求时出现错误java.io.BufferedInputStream.streamClosed(BufferedInputStream.java:125)
,但图像已成功上传,这是我要转换InputStream
为的函数byte[]
,
public static byte[] streamToBytes(InputStream is) {
ByteArrayOutputStream os = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len=0;
try {
while ((len = is.read(buffer)) >= 0) {
os.write(buffer, 0, len);
}
os.flush();
os.close();
is.close();
} catch (java.io.IOException e) {
}
return os.toByteArray();
}