首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Angular HttpClient中的Spring Boot StreamingResponseBody

使用Angular HttpClient中的Spring Boot StreamingResponseBody
EN

Stack Overflow用户
提问于 2019-07-31 15:00:07
回答 1查看 816关注 0票数 0

我有一个Spring Boot应用程序,它服务于100张压缩并通过StreamingResponseBody发送的图像,有没有办法从angular应用程序中使用这个服务?

代码语言:javascript
复制
@GetMapping(value = "/initialize")
public ResponseEntity<StreamingResponseBody> initializeSlider(final HttpServletResponse response,
                                                              String axis,String nodeId) {
    Long totalCount = imageRepository.countImageByAxisAndNodeId(axis,nodeId);

    int interval =  Math.round(totalCount / 50) ;

    List<Integer> sequenceList = new ArrayList<>();

    for(int i=0;i<totalCount;i=i+interval){
        int nextSequence = i + interval;
        sequenceList.add(nextSequence);
    }

    System.out.println(sequenceList.toString());
    System.out.println(sequenceList);

    response.setContentType("application/zip");
 //   response.setHeader("Content-Disposition", "attachment;filename=sample.zip");

    StreamingResponseBody stream = out -> {
        final ZipOutputStream zipOut = new ZipOutputStream(response.getOutputStream());
        List<Image> imageList = imageRepository.findImageBySequenceIdIn(sequenceList);

        try {
            imageList.stream().forEach(image -> {
                try {
                    ByteArrayOutputStream bos = new ByteArrayOutputStream();
                    ObjectOutputStream oos = new ObjectOutputStream(bos);
                    oos.writeObject(image);
                    oos.flush();
                    byte [] data = bos.toByteArray();
                    final InputStream inputStream = new ByteArrayInputStream(data);
                    final ZipEntry zipEntry = new ZipEntry(image.getName());
                    zipOut.putNextEntry(zipEntry);
                    //IOUtils.copy(inputStream,zipOut);
                    byte[] bytes = new byte[1024];
                    int length;
                    while ((length = inputStream.read(bytes)) >= 0) {
                        zipOut.write(bytes, 0, length);
                    }
                    zipOut.closeEntry();
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            });
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            zipOut.flush();
            zipOut.close();
        }
    };

    System.out.println(stream);
    logger.info("steaming response {} ", stream);
    return new ResponseEntity(stream, HttpStatus.OK);
}
EN

回答 1

Stack Overflow用户

发布于 2019-07-31 15:19:40

您可以尝试使用{ responseType: 'blob' }作为选项。

要下载该文件,可以使用file-saver

import { saveAs } from 'file-saver';

代码语言:javascript
复制
this.http.get(url, { responseType: 'blob' }).subscribe((resp: any) => {
    FileSaver.saveAs(resp, `abc.zip`);
})
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57284959

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档