英文:
How to set file size in the header in spring boot to display the download progress?
问题
如何在Spring Boot中设置header
中的文件大小
以在客户端显示下载进度?
@GetMapping(value = "/document/{nodeRef}/{versionNumber}")
public byte[] getDocumentWithVersion(@PathVariable(value = "nodeRef") UUID nodeRef, @PathVariable(value = "versionNumber") Double versionNumber) {
return docService.getDocument(nodeRef, versionNumber);
}
英文:
How to set file size
in the header
in spring boot to display the download progress in the client?
@GetMapping(value = "/document/{nodeRef}/{versionNumber}")
public byte[] getDocumentWithVersion(@PathVariable(value = "nodeRef") UUID nodeRef, @PathVariable(value = "versionNumber") Double versionNumber) {
return docService.getDocument(nodeRef, versionNumber);
}
答案1
得分: 1
The client receives the header
before the file
or the body
starts the download. So added Content-length
and content-type
in the header
.
@GetMapping(value = "/document/{nodeRef}/{versionNumber}")
public ResponseEntity<byte[]> getDocumentWithVersion(@PathVariable(value = "nodeRef") UUID nodeRef, @PathVariable(value = "versionNumber") Double versionNumber) {
byte[] file = alfrescoService.getDocument(null, nodeRef, versionNumber);
HttpHeaders headers = new HttpHeaders();
headers.add(HttpHeaders.CONTENT_LENGTH, String.valueOf(file.length));
headers.add(HttpHeaders.CONTENT_TYPE, "application/octet-stream");
return ResponseEntity.ok().headers(headers).body(file);
}
英文:
The client receives the header
before the file
or the body
starts the download. So added Content-length
and content-type
in the header
.
@GetMapping(value = "/document/{nodeRef}/{versionNumber}")
public ResponseEntity<byte[]> getDocumentWithVersion(@PathVariable(value = "nodeRef") UUID nodeRef, @PathVariable(value = "versionNumber") Double versionNumber) {
byte[] file = alfrescoService.getDocument(null, nodeRef, versionNumber);
HttpHeaders headers = new HttpHeaders();
headers.add(HttpHeaders.CONTENT_LENGTH, String.valueOf(file.length));
headers.add(HttpHeaders.CONTENT_TYPE, "application/octet-stream");
return ResponseEntity.ok().headers(headers).body(file);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论