Ejemplo 1: punto final para cargar y recuperar imágenes en la base de datos usando Spring Boot
<%@page contentType="text/html" pageEncoding="UTF-8"%><!DOCTYPE html><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>JSP Page</title></head><body><h1>Details are given below:</h1><hr><table><tr><td>Id</td><td>$id</td></tr><tr><td>Name</td><td>$name</td></tr><tr><td>Image</td><td><img src="data:image/jpg;base64,$image alt="Girl in a jacket" style="width:50px;height:50px;"></img></td></tr></table></body></html>
Ejemplo 2: punto final para cargar y recuperar imágenes en la base de datos usando Spring Boot
packagecom.vasu.SpringBootFileUpload.controller;importcom.vasu.SpringBootFileUpload.Model.MyModel;importcom.vasu.SpringBootFileUpload.Service.MyService;importjava.util.Base64;importorg.slf4j.Logger;importorg.slf4j.LoggerFactory;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Controller;importorg.springframework.ui.Model;importorg.springframework.web.bind.annotation.GetMapping;importorg.springframework.web.bind.annotation.ModelAttribute;importorg.springframework.web.bind.annotation.PathVariable;importorg.springframework.web.bind.annotation.PostMapping;importorg.springframework.web.bind.annotation.RequestBody;importorg.springframework.web.bind.annotation.RequestParam;importorg.springframework.web.bind.annotation.RestController;importorg.springframework.web.multipart.MultipartFile;/**
*
* @author Vasu Rajput
*/@ControllerpublicclassMyControllerprivatestaticfinalLogger logger =LoggerFactory.getLogger("MyController.class");@AutowiredprivateMyService myService;@GetMapping("/")publicStringtest()return"index";@PostMapping("/fileupload")publicStringfileUpload(@RequestParam("name")String name,@RequestParam("file")MultipartFile file)try
logger.info("Name= "+ name);byte[] image = file.getBytes();MyModel model =newMyModel(name, image);int saveImage = myService.saveImage(model);if(saveImage ==1)return"success";elsereturn"error";catch(Exception e)
logger.error("ERROR", e);return"error";@GetMapping("/getDetail/id")publicStringgetDbDetils(@PathVariableString id,Model model)try
logger.info("Id= "+ id);MyModel imagesObj = myService.getImages(Long.parseLong(id));
model.addAttribute("id", imagesObj.getId());
model.addAttribute("name", imagesObj.getName());byte[] encode =java.util.Base64.getEncoder().encode(imagesObj.getImage());
model.addAttribute("image",newString(encode,"UTF-8"));return"imagedetails";catch(Exception e)
logger.error("Error", e);
model.addAttribute("message","Error in getting image");return"redirect:/";
Te mostramos comentarios y calificaciones
No se te olvide recomendar este tutorial si te fue de ayuda.
¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)