将InputStream(Image)转换为ByteArrayInputStream [英] Convert InputStream(Image) to ByteArrayInputStream

查看:167
本文介绍了将InputStream(Image)转换为ByteArrayInputStream的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不确定我应该如何做到这一点。任何帮助将不胜感激

Not sure about how I am supposed to do this. Any help would be appreciated

推荐答案

从输入流中读取并写入 ByteArrayOutputStream ,然后调用它的 toByteArray()获取字节数组。

Read from input stream and write to a ByteArrayOutputStream, then call its toByteArray() to obtain the byte array.

创建 ByteArrayInputStream 从中读取。

这是一个快速测试:

import java.io.*;

public class Test {


       public static void main(String[] arg) throws Throwable {
          File f = new File(arg[0]);
          InputStream in = new FileInputStream(f);

          byte[] buff = new byte[8000];

          int bytesRead = 0;

          ByteArrayOutputStream bao = new ByteArrayOutputStream();

          while((bytesRead = in.read(buff)) != -1) {
             bao.write(buff, 0, bytesRead);
          }

          byte[] data = bao.toByteArray();

          ByteArrayInputStream bin = new ByteArrayInputStream(data);
          System.out.println(bin.available());
       }
}

这篇关于将InputStream(Image)转换为ByteArrayInputStream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆