【发布时间】:2020-09-19 00:13:19
【问题描述】:
我有一个将数据块写入响应的后端(Spring Boot)API。我正在尝试从我的 Angular 代码中读取它。我期望的是从服务器接收大块数据。但是,httpClient 的 get 方法在从服务器检索到所有数据之前不会返回任何内容。我已经给出了下面的代码
Spring Boot 代码
@GetMapping(value = "/stream", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
public ResponseEntity<StreamingResponseBody> fetchData(@RequestParam String path) {
try {
Pair<InputStream, Long> streamAndLen = CommonUtils.getTestStream();
InputStream stream = streamAndLen.getKey();
StreamingResponseBody resp = outputStream -> {
CommonUtils.copy(stream, outputStream);
stream.close();
};
return ResponseEntity.ok()
.header(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS, HttpHeaders.CONTENT_TYPE)
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_OCTET_STREAM_VALUE)
.contentLength(streamAndLen.getValue())
.contentType(MediaType.APPLICATION_OCTET_STREAM)
.body(resp);
} catch (Exception e) {
throw new RuntimeException("Could not get the data for the requested file", e);
}
}
==============CommonUtils==================
public static Pair<InputStream, Long> getTestStream() {
File file = new File("file_path_in_system");
try {
return new Pair<>(new FileInputStream(file), file.length());
} catch (FileNotFoundException e) {
e.printStackTrace();
throw new RuntimeException("File " + file.getAbsolutePath() + " is not found.", e);
}
}
public static void copy(InputStream stream, OutputStream outputStream) {
try {
int buffSize = 1024 * 4;
byte[] data = new byte[buffSize];
int length = -1;
while ((length = stream.read(data)) != -1) {
outputStream.write(data, 0, length);
outputStream.flush();
}
} catch (IOException e) {
e.printStackTrace();
}
}
Pair 只是一个键和值 POJO。
角度 sn-p
let headers = new HttpHeaders().set('Accept', 'application/octet-stream');
this.http.get(env.streamURL + '?path=' + this.filePath, { headers, observe: 'body', reportProgress: true, responseType: 'blob' })
.pipe(
map(res => {
console.log(res);
})
)
.subscribe(
(res: any) => {
console.log('res = ' + res);
}
);
我想在我的 Angular 应用程序中获取数据块(在每次从服务器调用 flush 之后),而不是等待所有块。有没有办法实现?
【问题讨论】:
标签: angular stream httpclient chunks