有时我们需要重复利用输入流,比如图片上传时获取 图片的宽高…… 还有很多………
1、代码示例
1 2 3 4 5 6 7 8 9 10
| InputStream in = files[i].getInputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len; while ((len = in.read(buffer)) > -1 ) { baos.write(buffer, 0, len); } baos.flush(); InputStream input = new ByteArrayInputStream(baos.toByteArray());
|
2、重复利用
一般做图片上传之类的用得挺多的
1 2 3 4 5 6 7 8
| int height = "0"; int width = "0"; BufferedImage bi = ImageIO.read(new ByteArrayInputStream(baos.toByteArray())); if (bi != null) { height = bi.getHeight(); width = bi.getWidth(); System.out.println("height=" + height + ",width=" + width); }
|