从数据库生成的胸腺和春季图像-会遇到奇怪的行为

问题描述 投票:0回答:1

我正在做一个项目,我正在将Thymeleaf和Spring一起使用来创建网页,但是我似乎无法使图像生成正常工作。

我将它们保存在数据库中,因此可以很好地生成它们(到“生成的”-文件夹)。

enter image description here

因此,通过访问“项目”页面,图像应该在此处-但它们不会显示在Frontend上,直到我不切换Intellij并返回浏览器并再次单击刷新。我从未见过如此奇怪的行为。也许我在Thymeleaf中缺少什么?因为我是第一次使用它。

FILE_UPLOAD_ROOT-从application.properties中读取,它是src / main / resources / generated /

然后我的控制器将项目列表传递给此方法:

/**
 * Each entity that will extend {@link MediaModel} can use this method as a helper. In other words,
 * we should have these three fields added to the table that we're going to use:
 * pic(LONGBLOB), pic_name(VARCHAR), pic_type(VARCHAR)
 *
 * @param itemsList - database results for that we are going to generate images
 */
 public static void generateImages(Iterable<? extends MediaModel> itemsList) {

    itemsList.forEach(item -> {
        try {
            if (item.getPicName() == null) {
                return;
            } else {
                Files.write(Paths.get(FILE_UPLOAD_ROOT + item.getPicName() + "." + item.getPicType()), item.getPic());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    });
}

在Thymeleaf中,我有:我不确定为什么,但是图片不是直接从generated文件夹拍摄,我应该仅指定根目录,无论如何Thymeleaf根本无法显示它们。

<img th:src="@{/{picName}.{picType}(picName=${project.picName},picType=${project.picType})}"/>

保存项目图像的MySQL模式

create table projects
(
    id          bigint       not null
        primary key,
    created_at  datetime(6)  not null,
    updated_at  datetime(6)  not null,
    pic         longblob     null,
    pic_name    varchar(255) null,
    pic_type    varchar(255) null,
    category_id bigint       not null,
    constraint FKmagkis2tpqxxx8hq100rjo92v
        foreign key (category_id) references projects_categories (id)
            on delete cascade
);

enter image description here

并且当我尝试访问页面时,只有在不切换回Intellij的情况下:

enter image description here

P.S。是的,我尝试将其部署到远程服务器,根本看不到它们,但是我查看了目标文件夹,并且图像在那里。

mysql spring-boot thymeleaf
1个回答
0
投票

为什么要从数据库将图像存储到压缩的warjar内部的文件夹中?不理想

要么将打包存储的映像存储在服务器上,然后在服务器上使用它。

OR

只需如下使用图像标签并实时渲染图像

<img alt="img"  th:src="@{'data:image/jpeg;base64,'+${Base64EncodedImage}}" />
© www.soinside.com 2019 - 2024. All rights reserved.