0

I need to save a Image into local folder and download it after using Spring Boot in both cases. On my DB I save only the name of the file.

I save the image with this:

   @PostMapping("/saveimage")
   public ResponseEntity<Usuario> insertProduct(@RequestPart("body") String body,
            @RequestPart("imagen") MultipartFile imagen) {
        //......
    }

But now, I need to recover it with a get request to work on other app.

How could I recover this? Only with URL I can't because both applications are on different servers so I need to do it with GetRequest.

I've tried this:

    @GetMapping("/getimage/{path}")
    ResponseEntity<File> imagen(@PathVariable String path) {
        File file = new File(this.uploadingDir + path);
        return new ResponseEntity<File>(file, HttpStatus.OK);   
    }

But it doesn't work properly, I get the full path not the file.

Alexey Usharovski
  • 1,404
  • 13
  • 31
SantiSori
  • 381
  • 2
  • 15

2 Answers2

0

Class File contains only info about path to the file and nothing more. To read the data from it you should create a stream from that file object.

Try to use something like that.

@GetMapping("/getimage/{path}")
public void adminDownloadProductPicture(@PathVariable("path") String path,
                                        HttpServletResponse response) throws IOException {
    File file = new File(this.uploadingDir + path); // or you can use Paths.get(this.uploadingDir + path); instead
    Files.copy(file.toPath(), response.getOutputStream());
    response.setContentType("image/jpeg"); // your content type here
 }

Notice that you should set the mime type (content type) according to your picture (image/jpeg, image/gif, image/bmp etc.)

Also please read this article for details and other ways https://www.baeldung.com/spring-mvc-image-media-data

Alexey Usharovski
  • 1,404
  • 13
  • 31
0

I also had a similar UseCase. The below code is working well for me. I get the filename from the Primary Key Associate with File/Image (below ZIP file). As you have a path of Image in PathVaraiabe you can change according to that. Also, change the Content-Type and produces according to your need. Possible Content-Type

@RequestMapping(value="/download/{assetId}",method=RequestMethod.GET,produces="application/zip" )
    public ResponseEntity<?> downloadAsset(@PathVariable("assetId") BigInteger assetId, HttpServletResponse response) throws IOException
    {

             String filename = assetService.downloadAssetName(assetId);
             if(filename!=null && !filename.isEmpty())
             {
                File file = new File(uploadingDir + filename); 
                InputStreamResource resource2 = new InputStreamResource(new FileInputStream(file));
                response.setContentType("application/zip");
                response.setHeader("Content-Disposition", String.format("inline; filename=\"" + filename + "\""));
                response.setHeader("responseType", "arraybuffer");
                response.setHeader("Content-Length", ""+file.length());

                return new ResponseEntity<InputStreamResource>(resource2,HttpStatus.ACCEPTED);
             }
             else
                 return new ResponseEntity<String>("message",HttpStatus.BAD_REQUEST);
    }
Romil Patel
  • 12,879
  • 7
  • 47
  • 76