java.io.IOException: stream is closed

下载的地方有断点就是导致stream关闭---java.io.IOException: stream is closed
    
    public Integer downFileJpg(String filePath) throws Exception {
        // feign文件下载
        File file =new File(localCache);
        //如果文件夹不存在则创建
        if (!file.exists() && !file.isDirectory()){
            log.info("//不存在");
            file.mkdir();
        }
        log.info("文件路径:{},请求token:{}",filePath,localToken);
        Response response = exClient.downData(filePath,localToken);
        if(response.status() != 200){
            log.error("调用s下载异常:{},文件路劲:{}",response.reason(),filePath);
            throw new BException("调用s下载异常",response.status());
        }
        String fileName = filePath.substring(filePath.lastIndexOf("/")+1);
        Response.Body body = response.body();
        // 构建输出流,将输入流写入到输出流
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        try (InputStream inputStream = body.asInputStream();) {
            byte[] buf = new byte[10240];
            while (true) {
                int len = inputStream.read(buf);
                if (len < 0) {
                    break;
                }
                bos.write(buf, 0, len);
            }
            //将输出流转为字节数组,通过ResponseEntity<byte[]>返回
            byte[] by = bos.toByteArray();
            log.info("接收到的文件大小为:{}",by.length);
            FileUtils.writeByteArrayToFile(new File(localCache+"/"+fileName), by);
        } catch (IOException e) {
            e.printStackTrace();
            log.error("调用supcloud下载异常:{},文件路劲:{}",e.getMessage(),filePath,e);
            throw new BException("调用s下载异常",Re.INTERNAL_ERROR.getCode());
        }finally {
            bos.close();
        }
        log.info("执行完毕》》》》》");
        return 1;
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值