[英]Load Image/Files from Spring Boot Rest api To the Android front end
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.这是我的 controller 端点,它将使用文件名从前端获取请求并返回使用该文件名的图像/文件。 It works fine while testing on postman but I have no idea how to load images/files from this end point in android.
它在 postman 上测试时工作正常,但我不知道如何从 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.我需要从这里加载图像/文件到 android 前端。
Picasso is the option to do this.毕加索是执行此操作的选项。 just load your images with the resource you receive from server...
只需使用从服务器接收到的资源加载图像...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.