Issue
This is my controller end point that will take the request from the frontend with the filename and using that filename image/file shall be returned. It works fine while testing on postman but I have no idea how to load images/files from this end point in android.
GetMapping("downloadFile/{fileName}")
@PreAuthorize("hasRole('ADMIN') or hasRole('MODERATOR') or hasRole('USER')")
public ResponseEntity<Resource> downloadFile(@PathVariable String fileName, HttpServletRequest request) {
Resource resource = fileStorageService.loadFileAsResource(fileName);
//System.out.println(resource);
String contentType = null;
try {
contentType = request.getServletContext().getMimeType(resource.getFile().getAbsolutePath());
} catch (IOException ex) {
ex.printStackTrace();
}
if (contentType == null) {
contentType = AppConstants.DEFAULT_CONTENT_TYPE;
}
return ResponseEntity.ok().contentType(MediaType.parseMediaType(contentType))
.header(HttpHeaders.CONTENT_DISPOSITION,
String.format(AppConstants.FILE_DOWNLOAD_HTTP_HEADER, resource.getFilename()))
.body(resource);
}
And My service for this looks like..
public Resource loadFileAsResource(String fileName) {
try {
//Path filePath = this.fileStorageLocation.resolve(fileName).normalize();
Path filePath= this.fileStoragePath.resolve(fileName).normalize();
Resource resource = new UrlResource(filePath.toUri());
if (resource.exists()) {
return resource;
} else {
throw new FileStorageException(AppConstants.FILE_NOT_FOUND + fileName);
}
} catch (MalformedURLException ex) {
throw new FileStorageException(AppConstants.FILE_NOT_FOUND + fileName, ex);
}
}
I need to load image/files from here to the android frontend.
Solution
Picasso is the option to do this. just load your images with the resource you receive from server...
Answered By - Invincible System
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.