如何在Angular 8中使用spring StreamingResponseBody

时间:2020-02-12 01:31:34

标签: java angular rest spring-boot streaming

我的springboot后端中有以下代码

import re

s = '12 123 1234 12345 123456 1234567 12345678'
print(re.sub(r'(\d{3}\b)', r',\1', s))

# gives
# 12 ,123 1,234 12,345 123,456 12345,678

我试图像这样在我的角度应用程序中使用它

HTML

@GetMapping(value = "/stream", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE) public ResponseEntity<StreamingResponseBody> stream() { File file = new File("C:\\test\\test.mp3"); try { InputStream is = new FileInputStream(file); StreamingResponseBody resp = outputStream -> { IOUtils.copy(is, outputStream); }; return ResponseEntity.ok() .contentType(MediaType.APPLICATION_OCTET_STREAM) .contentLength(file.length()) .body(resp); } catch(Exception e) { e.printStackTrace(); } return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) .contentType(MediaType.APPLICATION_OCTET_STREAM) .body(null); }

打字稿

<p>{{ content }}</p>

但是,除非我为get方法指定ngOnInit(): void { const header = new HttpHeaders().set('Accept', 'application/octet-stream'); this.http.get('http://localhost:8080/stream', {headers: header}).subscribe( (res: any) => { this.content = JSON.stringify(res); }, (err: any) => { this.content = JSON.stringify(err); } ); } ,否则只会得到错误。另外,我尝试将responseType指定为blob和arraybuffer,并指定了responseType: 'text'的几种组合。页面上仅显示observe: body|response|events。不知道这里出了什么问题。

我尝试关注this SO post,但用处不大。 有人可以请问一下吗?提前致谢。

0 个答案:

没有答案