Skip to content

Instantly share code, notes, and snippets.

@Ikhiloya
Created July 20, 2020 23:22
Show Gist options
  • Save Ikhiloya/5eb82e289b138469a05146698b23a289 to your computer and use it in GitHub Desktop.
Save Ikhiloya/5eb82e289b138469a05146698b23a289 to your computer and use it in GitHub Desktop.
@Override
public ResponseEntity<Object> downloadFile(String fileName, HttpServletRequest request) throws Exception {
Storage storage = storageOptions.getService();
Blob blob = storage.get(BlobId.of(bucketName, fileName));
ReadChannel reader = blob.reader();
InputStream inputStream = Channels.newInputStream(reader);
byte[] content = null;
log.info("File downloaded successfully.");
content = IOUtils.toByteArray(inputStream);
final ByteArrayResource byteArrayResource = new ByteArrayResource(content);
return ResponseEntity
.ok()
.contentLength(content.length)
.header("Content-type", "application/octet-stream")
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + fileName + "\"")
.body(byteArrayResource);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment