下载的地方有断点就是导致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;
}
java.io.IOException: stream is closed
最新推荐文章于 2024-09-13 14:48:49 发布