如何在Spring Boot中设置文件大小以在标头中显示下载进度?

huangapple go评论87阅读模式
英文:

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 = &quot;/document/{nodeRef}/{versionNumber}&quot;)
public ResponseEntity&lt;byte[]&gt; getDocumentWithVersion(@PathVariable(value = &quot;nodeRef&quot;) UUID nodeRef, @PathVariable(value = &quot;versionNumber&quot;) 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, &quot;application/octet-stream&quot;);
	return ResponseEntity.ok().headers(headers).body(file);
}

huangapple
  • 本文由 发表于 2020年7月28日 10:32:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/63126198.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定