Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring boot 从rest模板异步读取InputStream,以便使用ParallelScatterZipCreator压缩文件_Spring Boot_Zip - Fatal编程技术网

Spring boot 从rest模板异步读取InputStream,以便使用ParallelScatterZipCreator压缩文件

Spring 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

正在尝试创建InputStreamSupplier,并通过调用restTemplate获取文件列表。以InputStream作为响应的。但是在future.get()时获取异常NullPointerException

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;
}