似乎我错过了带有标题的arround,在保存时,这是最终版本,它可能会帮助其他人:
将这些配置添加到
:
@Configuration
public static class WebConfig extends WebMvcConfigurerAdapter {
@Override
public void configureAsyncSupport(AsyncSupportConfigurer configurer) {
configurer.setDefaultTimeout(-1);
configurer.setTaskExecutor(asyncTaskExecutor());
}
@Bean
public AsyncTaskExecutor asyncTaskExecutor() {
return new SimpleAsyncTaskExecutor("async");
}
}
这是给你的控制器的:
@RequestMapping(value = "{analyseId}/download", method = RequestMethod.GET, produces = "application/zip")
public ResponseEntity<StreamingResponseBody> download(@PathVariable Long analyseId) throws IOException {
try {
Analyse analyse = analyseService.getAnalyse(analyseId);
final InputStream file =azureDataLakeStoreService.readFile(analyse.getZippedFilePath());
Long fileLength = azureDataLakeStoreService.getContentSummary(analyse.getZippedFilePath()).length;
StreamingResponseBody stream = outputStream ->
readAndWrite(file , outputStream);
String zipFileName = FilenameUtils.getName(analyse.getZippedFilePath());
return ResponseEntity.ok()
.header(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS, HttpHeaders.CONTENT_DISPOSITION)
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + zipFileName)
.contentLength(fileLength)
.contentType(MediaType.parseMediaType("application/zip"))
.body(stream);
} catch (Exception e) {
e.printStackTrace();
return ExceptionMapper.toResponse(e);
}
}
private void readAndWrite(final InputStream is, OutputStream os)
throws IOException {
byte[] data = new byte[2048];
int read = 0;
while ((read = is.read(data)) >= 0) {
os.write(data, 0, read);
}
os.flush();
}
download(id) {
let url = URL + '/analyses/' + id + '/download';
const headers = new HttpHeaders().set('Accept', 'application/zip');
const req = new HttpRequest('GET', url, {
headers: headers,
responseType: 'blob',
observe: 'response',
reportProgress: true,
});
const dialogRef = this.dialog.open(DownloadInProgressDialogComponent);
this.http.request(req).subscribe(event => {
if (event.type === HttpEventType.DownloadProgress) {
dialogRef.componentInstance.progress = Math.round(100 * event.loaded / event.total) // download percentage
} else if (event instanceof HttpResponse) {
dialogRef.componentInstance.progress = 100;
this.saveToFileSystem(event, 'application/zip');
dialogRef.close();
}
});
}
private saveToFileSystem(response, type) {
const contentDispositionHeader: string = response.headers.get('Content-Disposition');
const parts: string[] = contentDispositionHeader.split(';');
const filename = parts[1].split('=')[1];
const blob = new Blob([response.body], {
type: type
});
saveAs(blob, filename);
}