gpt4 book ai didi

mysql - 从数据库生成 Thymeleaf 和 Spring 图像 - 将遇到奇怪的行为

转载 作者:行者123 更新时间:2023-11-29 15:22:57 25 4
gpt4 key购买 nike

我正在开发一个项目,我正在使用 Thymeleaf 和 Spring 来创建网页,但我似乎无法正常生成图像。

我的数据库中有它们,所以它们生成得很好(到“生成的”文件夹)。

enter image description here

因此,通过访问“项目”页面,图像应该在那里 - 但它们不会显示在前端,直到我不切换 Intellij 并返回到浏览器并再次单击“刷新”。我从未见过如此奇怪的行为。也许我在 thymeleaf 中遗漏了一些东西?因为我是第一次使用它。

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

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

/**
* 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 中我有:我不太清楚为什么,但图片不是直接从 生成的文件夹中获取的,我应该只指定根目录,无论如何 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

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

最佳答案

为什么要将图像从数据库存储到压缩的 warjar 内的文件夹中?这是不可取的

将可部署的打包镜像存储在服务器上并从那里使用它。

或者

只需使用如下图像标签并动态渲染图像

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

P.S. - 在 IDE 中工作与在服务器上使用应用程序不同。您的方法可以在 IDE 中运行,但不能在服务器上运行

关于mysql - 从数据库生成 Thymeleaf 和 Spring 图像 - 将遇到奇怪的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59256053/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com