3

I'm getting an OutOfMemoryError exception while reading image file from disk folder. What I do:

@GetMapping(value = "/user/image")
    public ResponseEntity<byte[]> identificationImage(@RequestParam String path, @RequestParam String filename) {
        byte[] image = Files.readAllBytes(Paths.get(path, filename));

        return ResponseEntity.ok().contentType(MediaType.IMAGE_JPEG).body(image);
    }

Is there a way to read files without loading into memory or something?

Out of memory error in a spring boot + spring data web application.

lambodar
  • 3,495
  • 5
  • 34
  • 58
  • What's the size of the image you want to load and what does `Paths.get(path, filename)` evaluate to? Are you sure it is the image and not the directory? – hotzst Nov 22 '19 at 13:40
  • [This](https://stackoverflow.com/questions/35680932/download-a-file-from-spring-boot-rest-service) might help. – Andrew S Nov 22 '19 at 13:41
  • What do you need to do with the image? Would splitting up the image & processing it chunk by chunk (by streaming the image data) be possible, or do you need to display the full image? – Vince Nov 22 '19 at 13:42
  • @hotzst 10MB. Path evaluates ok (/var/storage/images/user_image.jpg) – Alejandro Torres Nov 22 '19 at 13:53
  • @VinceEmigh Yes I need to display in my backoffice. Would I have to increase my memory max size or is there a way to handle this without loading the image into the memory all at once? thanks – Alejandro Torres Nov 22 '19 at 13:54
  • @hotzst image is 700KB sorry – Alejandro Torres Nov 22 '19 at 15:27
  • Maybe you can split the image, so you only have to load a fraction at a time. You'd load one slice, draw it, load another slice, draw, etc... In the end, you'll need enough memory to handle the *drawing* of the full image either way, so increasing your memory is probably the easiest and safest option. – Vince Nov 22 '19 at 15:31

0 Answers0