Spring boot 从rest模板异步读取InputStream,以便使用ParallelScatterZipCreator压缩文件
正在尝试创建InputStreamSupplier,并通过调用restTemplate获取文件列表。以InputStream作为响应的。但是在future.get()时获取异常NullPointerExceptionSpring boot 从rest模板异步读取InputStream,以便使用ParallelScatterZipCreator压缩文件,spring-boot,zip,Spring Boot,Zip,正在尝试创建InputStreamSupplier,并通过调用restTemplate获取文件列表。以InputStream作为响应的。但是在future.get()时获取异常NullPointerException lists.forEach(obj->{ InputStreamSupplier streamSupplier=()->{ InputStream=null; 试一试{ is=doDownload(obj.getId()); }捕获(例外e){ e、 printStackTrace
lists.forEach(obj->{
InputStreamSupplier streamSupplier=()->{
InputStream=null;
试一试{
is=doDownload(obj.getId());
}捕获(例外e){
e、 printStackTrace();
}
回报是;
};
InputStream doDownload(长id){
InputStream InputStream=null;
试一试{
ResponseEntity response=restemplate.exchange(url,HttpMethod.GET,requestEntity,byte[].class);
inputStream=newbytearrayinputstream(response.getBody());
}捕获(异常扩展){
log.error(ExceptionUtils.getMessage(exp));
}
返回输入流;
}
scatterZipCreator.addArchiveEntry(zipArchiveEntry,streamSupplier);//在使用future.get()方法获取流时给出nullPointerException。bz inputstream正在正确获取。bz doDownload由线程通过视差调用。这种行为是一致的。有时会发生。任何克服这种情况的方法都可以
lists.forEach(obj -> {
InputStreamSupplier streamSupplier = () -> {
InputStream is = null;
try {
is = doDownload(obj.getId());
} catch (Exception e) {
e.printStackTrace();
}
return is;
};
InputStream doDownload(Long id){
InputStream inputStream = null;
try {
ResponseEntity<byte[]> response = restTemplate.exchange(url, HttpMethod.GET,requestEntity, byte[].class);
inputStream = new ByteArrayInputStream(response.getBody());
} catch (Exception exp) {
log.error(ExceptionUtils.getMessage(exp));
}
return inputStream;
}