In code, this error occurs when writing a photo to the database. How to solve it?
class ImageController:
package com.example.web_start.controllers;
import com.example.web_start.models.Image;
import com.example.web_start.repositories.ImageRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import java.io.ByteArrayInputStream;
@RestController
@RequiredArgsConstructor
public class ImageController {
private final ImageRepository imageRepository;
@GetMapping("/images/{id}")
private ResponseEntity<?> getImageById(@PathVariable Long id){
Image image = imageRepository.findById(id).orElse(null);
return ResponseEntity.ok()
.header("fileName",image.getOriginalFileName())
.contentType(MediaType.valueOf(image.getContentType()))
.contentLength(image.getSize())
.body(new InputStreamResource(new ByteArrayInputStream(image.getBytes())));
}
}