0

I created a class to upload an image from an thymeleaf html and saved it in a folder just outside my project. My question is, how can I render this images in my thymeleaf html from this new folder? The pictures are saved just fine, the problem is rendering the pictures in the html when i want to show them.

Here is the code for image uploading:

public class FileUpload {

    private final String CARPETA_UPLOAD = "..//imagenes//";
    private MultipartFile imagen;

    public FileUpload(MultipartFile imagen) {
        this.imagen = imagen;
    }

    public void subirImagen() {
        try {
            byte[] imgBytes = imagen.getBytes();
            Path path = Paths.get(CARPETA_UPLOAD + imagen.getOriginalFilename());
            crearCarpeta();
            Files.write(path,imgBytes);
            System.out.println("Si se guardo: " + imagen.getOriginalFilename() + " en el path: " + path.getParent());
        }catch(IOException e) {
            e.printStackTrace();
        }
    }

    private void crearCarpeta() throws IOException{

        if (!Files.exists(Paths.get(CARPETA_UPLOAD)))
            Files.createDirectory(Paths.get(CARPETA_UPLOAD));
    }

}

I didnt something similar in a JSP web page and to load the pictures I had to use something like this in my sun-web.xml : <property description="Portadas de canciones" name="portadasWeb" value="from=/images/* dir=./docroot/"/> but this was back then. Is there something similar in spring boot with thymeleaf that i can use to render the pictures?

TwoDent
  • 405
  • 7
  • 26

1 Answers1

0
<img  th:src="@{'data:image/jpeg;base64,'+${Cimage}}" 
/>

Alternatively, you can display image like below:

<img th:if="*{photo != null}" 
th:src="@{'data:image/jpg;base64,' + * 
{T(org.springframework.util.Base64Utils). 
 encodeToString(photo)}}"/>

See my question here: Store pictures in H2 database spring boot thymleaf

Vishal Torne
  • 366
  • 5
  • 24