'GridFS Download File location
I'm trying to download file from database using GridFS in Java-Spring.
So far I made this and this work fine, In postman everything work, I can say that I can download file, but I dont know where is my file downloaded, can I put specific location for download or something like that? How even GridFS download works?
This is my code for downloading file:
Controller:
@GetMapping("/downloadfile/{id}")
public ResponseEntity<ByteArrayResource> download(@PathVariable String id) throws IOException {
DocumentDto documentDto = documentService.downloadFile(id);
return ResponseEntity.ok()
.contentType(MediaType.parseMediaType(documentDto.getType()))
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + documentDto.getFileName() + "\"")
.body(new ByteArrayResource(documentDto.getFile()));
}
And service:
public DocumentDto downloadFile(String id) throws IOException {
GridFSFile gridFSFile = template.findOne(Query.query(Criteria.where("_id").is(id)));
DocumentDto documentDto = new DocumentDto();
if (gridFSFile != null && gridFSFile.getMetadata() != null) {
documentDto.setFileName(gridFSFile.getFilename());
documentDto.setType(gridFSFile.getMetadata().get("_contentType").toString());
documentDto.setFile(IOUtils.toByteArray(operations.getResource(gridFSFile).getInputStream()));
}
return documentDto;
}
Can I even do that? To put specific locations for saving files? Like on desktop or some directory on my C drive...
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
