简体   繁体   English

从 Spring 加载图像/文件 启动 Rest api 到 Android 前端

[英]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.

相关问题 春季前端无法从Rest API接收响应 - Unable to receive response from rest api in spring on front end React 前端未与 spring 引导 REST API 通信。控制台中的 Axios.network 错误 - React front end is not communicating with spring boot REST API. Axios network err in console 如何将标题和图像文件从前端 (React.js) 传递到后端 API (Spring Boot)? - How can I pass both the title and an image file from my front end (React.js) to my backend API (Spring Boot)? 无法从反应前端访问上传的图像到 spring 启动后端 - Unable to access uploaded image from react front-end to spring boot back-end 在 Google 前端负载均衡器后面运行时,如何正确从 Spring 引导 2.3 重定向到 HTTPS - How to properly Redirect from Spring Boot 2.3 to HTTPS when running behind a Google Front End Load Balancer 带有多线程的REST Api用于在Spring Boot中处理文件 - REST Api with Multithreading for handling Files in Spring Boot 如何通过rest api将静态文件提供给前端? - how to serve static files to a front end via rest api? 在 Spring Boot Rest Api 中返回一个图像 - Return an image in Spring Boot Rest Api Spring Boot和Jquery从前端的哈希映射访问值 - Spring Boot And Jquery accesing values from a hashmap on the front end 在来自Angular前端的一些频繁的API请求之后,httpSession.getAttribute(“ userId”)在Spring Boot中返回null - httpSession.getAttribute(“userId”) returns null in spring Boot after some frequent API request from Angular front end
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM