java 中 byte[]、File、InputStream 互相转换

转载:https://www.cnblogs.com/DylanZ/p/6269042.html

 

将inputstream转换成 byte【】

public static byte[] read(InputStream inputStream) throws IOException {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int num = inputStream.read(buffer);
while (num != -1) {
baos.write(buffer, 0, num);
num = inputStream.read(buffer);
}
baos.flush();
return baos.toByteArray();
} finally {
if (inputStream != null) {
inputStream.close();
}
}
}

 

posted @ 2018-10-24 15:59  ConfidentLiu  阅读(410)  评论(0编辑  收藏  举报