image work in springboot

 Bean file :

        @Lob

@Column(length = Integer.MAX_VALUE)

private byte[] images;

        public byte[] getImages() {

          return images;

           } 

        public void setImages(byte[] images) { 

         this.images = images; 

          }

save image controller :

     @PostMapping("/")

public String saveHolidayPackage(@ModelAttribute CreateHolidayPackageOne createHolidayPackageOne,

              @RequestParam("image") MultipartFile file,HttpSession httpSession)

throws IOException {

createHolidayPackageOne.setImages(file.getBytes());

createHolidayPackageOneRepo.save(createHolidayPackageOne);

return "Admin/CreateHolidayPackageDetails";

}


retrieve image from database controller :


         @GetMapping("/images1")

public void showimage1(@Param("id") long id,HttpServletResponse response,Optional<CreateHolidayPackageOne> holidayPackage) throws ServletException, IOException  {

    holidayPackage=createHolidayPackageOneRepo.findById(id);

response.setContentType("image/jpeg, image/jpg, image/png, image/gif");

 response.getOutputStream().write(holidayPackage.get().getImages());

response.getOutputStream().close();

}


HTML

th:src="@{'/images1?id='+${a1.id}}"

Comments