【发布时间】:2020-04-02 23:31:18
【问题描述】:
我正在做一个项目,我正在使用 Thymeleaf 和 Spring 创建网页,但我似乎无法正常生成图像。
我的数据库中有它们,所以它们生成得很好(到“生成”文件夹)。
因此,通过访问“项目”页面,图像应该在那里 - 但在我不切换 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
);
当我尝试访问该页面时,直到我不会切换回 Intellij:
附注是的,我尝试将其部署到远程服务器,根本看不到它们,但我查看了目标文件夹,图像就在那里。
【问题讨论】:
标签: mysql spring-boot thymeleaf