Get BLOB image from the database using Spring MVC + Thymeleaf

Asked

Viewed 118 times

1

I have problems trying to present an image on the frontend using Spring MVC and Hibernate to return the image stored in BLOB in the database.

Entity:

@Lob
@Column(length = 100000)
private byte[] foto;

public byte[] getFoto() {
    return foto;
}

public void setFoto(byte[] foto) {
    this.foto = foto;
}

Controller:

@GetMapping("/view")
    public ModelMap viewVehicle(@RequestParam(value = "id", required = false) Vehicle vehicle, Model model) {
        if (vehicle == null) {
            vehicle = new Vehicle();
        }

        return new ModelMap("vehicle", vehicle);
    }

HTML:

<img th:src="*{'data:image/png;base64,'+ foto}" />

I do not have any kind of error when compiling or running the application, not even accessing the page in question, however the image is not shown.

I tried several solutions presented on the Internet, but without success in any of them.

Some consideration to help me resolve this issue?

  • The language of the site is portugues, translate your question.

  • So.... the content of a byte[] is not the same as Base64

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.