欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

byte[]与InputStream互转 博客分类: Java javainputreambyte[]ByteArrayOutputStream 

程序员文章站 2024-02-24 20:42:46
...

InputStream转byte[]

 

	private byte[] InputStreamToByte(InputStream is) {
		try {
			ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
			int ch;
			while ((ch = is.read()) != -1) {
				bytestream.write(ch);
			}
			byte imgdata[] = bytestream.toByteArray();
			bytestream.close();
			return imgdata;
		} catch (IOException e) {
			return null;
		}
	}

 

byte[]转InputStream

 

byte[] data;
InputStream is = new ByteArrayInputStream(data);