Springboot does not render image (only restart)

2

I have an entity called product that has only Title, Price, and PathImage, which saves the path where the product image is saved. When I receive this form data, my service executes this code:

public void addProduto(Produto produto, MultipartFile imagem) {
    String caminho = "/img/" + produto.getTitulo().replaceAll(" ", "") + ".jpg";
    produto.caminhoImagem = caminho;
    UsingFileUtils.salvarImagem(caminho, imagem);

    repo.save(produto);
}

I remove all the title and command spaces for the UsingFileUtils class that does this:

public static void salvarImagem(String caminho, MultipartFile imagem) {
    File file = new File("src/main/resources/static"+caminho);
    try {
        FileUtils.writeByteArrayToFile(file,imagem.getBytes());
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

When doing this, it is redirected to the page with all products, but the image is not rendered, only if I restart the server, it appears there normally. In the html listing it looks like this:

<div class="card" style="width: 18rem;" th:each="produto : ${produtos}">
                <img class="card-img-top" th:src="@{${produto.caminhoImagem}}">
                <div class="card-body">
                    <h5 class="card-title" th:text="${produto.titulo}"></h5>
                    <p class="card-text" th:text="${produto.preco}"></p>
                    <a href="#" class="btn btn-primary">Quero</a>
                </div>
            </div>

What can I do to not have to restart the server to render the images?

    
asked by anonymous 08.06.2018 / 19:07

1 answer

0

The problem is that, according to this post , the static folder loads only when the application boots through Spring Boot, any changes to the contents of this folder will only be reflected on the next server boot.

That's right, possible solutions:

1) Place your images in another folder that sensitize Tomcat to restart automatically and, via Maven, copy them to the original static folder. This solution is here .

2) Use Spring Content , which automates this process of saving and recovering static resources in the Spring environment. Please read this post.

    
08.06.2018 / 19:44