java 将inputStream转为byte[]报Stream closed异常

public static byte[] toByteArray(InputStream inputStream) throws Exception {
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		byte[] buffer = new byte[1024*4];
		byte[] result = null;
		try {
			int n = 0;
            
			while ((n = inputStream.read(buffer)) != -1) {//这行就就近异常了
				out.write(buffer, 0, n);
			}
			result = out.toByteArray();
		} catch (Exception e) {
			LOG.error("将流转化为字节数组方法流出错"+e.getMessage());
			throw new Exception("将流转化为字节数组方法出错"+e.getMessage());
		}
		return result;
	}

检查一下传进来的inputStream参数是不是关闭了,或者之前就没有打开流,bytes应该是有问题的,inputStream的流要不从网络来的,要不就是从文件来的,文件来的应该是传文件字符串参数,网络来的应该是从socket获取。

while ((n = inputStream.read(buffer)) != -1) {//这行就就近异常了

这行报错的话,就不是这个方法的问题了,而你是传进来的 InputStream 有问题了吧

public class Test {

  public static void main(String[] args) throws Exception {
    // 随机生成字节数组,不是 java 基本库里面的。hutool 中的
    byte[] bytes = RandomUtil.randomBytes(20);
    System.out.println(Arrays.toString(bytes));
    bytes = toByteArray(new ByteArrayInputStream(bytes));
    System.out.println(Arrays.toString(bytes));
  }

  public static byte[] toByteArray(InputStream inputStream) throws Exception {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024*4];
    byte[] result = null;
    try {
      int n = 0;

      while ((n = inputStream.read(buffer)) != -1) {//这行就就近异常了
        out.write(buffer, 0, n);
      }
      result = out.toByteArray();
    } catch (Exception e) {
      throw new Exception("将流转化为字节数组方法出错"+e.getMessage());
    }
    return result;
  }

}
/*
[107, -26, 126, 48, -41, -16, 102, 16, 112, 111, -121, 36, -75, -59, -102, -121, 3, 90, 76, -13]
[107, -26, 126, 48, -41, -16, 102, 16, 112, 111, -121, 36, -75, -59, -102, -121, 3, 90, 76, -13]
*/

 

轻松学JAVA:https://edu.csdn.net/course/detail/2981

Stream closed 这异常应该是你调用toByteArray方法的时候,入参inputStream 异常关闭了。你只贴这个方法看不出什么东西的。

	CloseableHttpResponse response = null;
		try {
			response = client.execute(httpGet);//由客户端执行(发送)Post请求
			if (response != null) {
				HttpEntity resEntity = response.getEntity();//从响应模型中获取响应实体
				if (resEntity != null) {
						//EntityUtils.toString(resEntity)这个方法会关闭流该怎么处理呀,
					  try { imageBase64 = JSONObject.parseObject(EntityUtils.toString(resEntity));
					  LOG.error("接口返回为标准json,说明验证码获取失败,直接返回"); return imageBase64;
					  
					  } catch (Exception e) { LOG.info("未返回标准json传,开始进行验证码文件流处理"); }
					 
					 
					InputStream inputStream=resEntity.getContent();
					String base64String= FileStrUtil.streamToString(inputStream);
					if(base64String!=null&&!(base64String.equals("")))
					imageBase64.put("imageBase64", base64String);
					else {
						LOG.error("INFO返回图片数据为空");
						throw new Exception("INFO返回图片数据为空");
					}
				}
			}
		} catch (Exception e) {
			LOG.error("外呼失败", e.getMessage());
			throw new Exception("外呼失败  Msg:["+e.getMessage()+"]");
		} finally {

EntityUtils.toString(resEntity)这个方法会关闭流怎么办呀,接口成功返回的是流文件,失败返回是个json串提示失败原因

您好,我是有问必答小助手,你的问题已经有小伙伴为您解答了问题,您看下是否解决了您的问题,可以追评进行沟通哦~

如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~

ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632