java - 图像未从数据库检索到 html 编辑表单
问题描述
使用 java、spring boot 和 html,我创建了一个小网站,在 html 和多部分文件的帮助下,我在首页显示了图像
在控制器中,我给出了私有字符串图像;在 entity.class 中,我在 mysql 数据库中创建了 image 列作为 varchar() 类型。
图像路径保存到数据库中,图像显示在 html 主页中,但问题是当我尝试编辑图像时,显示了 edit_form,但在图像字段中,图像路径未从数据库中显示。从数据库中检索其他文本字段,但未检索图像字段路径。
我还尝试 th:field=${} 从数据库中检索图像,但显示原因:org.thymeleaf.exceptions.TemplateProcessingException:执行处理器 'org.thymeleaf.spring5.processor.SpringInputFileFieldTagProcessor 时出错。
Stack Overflow 上也有类似的问题。但我找不到我的问题的正确答案。我已经尝试了很多方法来解决这个问题,但对我来说没有任何效果。
所以,谁能告诉我如何解决这个问题。
1.edit_form.html
<main class="container">
<div class="row">
<div class="starter-template mt-5 col-lg-12" align="center">
<form th:action="@{/home}" th:object = "${gameofthrones}" method="POST" enctype="multipart/form-data">
<div class = "form-group" >
<label> Image </label>
<input
type = "file"
name = "fileImage"
class = "form-control"
id = "fileImage"
accept="image/png, image/jpeg"
/>
<br/>
<a class="nav-link" th:href="got-images+'/'+${id} + '/' + ${image}">LINK TEST</a>
</div>
2.Controller.Class
@GetMapping("/got/edit/{id}")
public String editGotForm(@PathVariable Integer id, Model model)
{
Gameofthrones gameofthrones = new Gameofthrones();
gameofthrones = gotService.getById(34);
model.addAttribute("gameofthrones", gameofthrones);
return "edit_got";
}
@RequestMapping(value="/home",method = RequestMethod.POST)
public String saveGot(Model model,@ModelAttribute("gameofthrones") Gameofthrones gameofthrones, @RequestParam("fileImage") MultipartFile multipartFile) throws IOException
{
String fileName = StringUtils.cleanPath(multipartFile.getOriginalFilename());
gameofthrones.setImage(fileName);
Gameofthrones saveGot = gotService.savGot(gameofthrones);
String uploadDir = "./got-images/" + saveGot.getId();
Path uploadPath = Paths.get(uploadDir);
if(!Files.exists(uploadPath)) {
Files.createDirectories(uploadPath);
}
try (InputStream inputStream = multipartFile.getInputStream()) {
Path filePath = uploadPath.resolve(fileName);
System.out.println(filePath.toFile().getAbsolutePath());
Files.copy(inputStream, filePath, StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
throw new IOException("Could not save uploaded file: " + fileName);
}
System.out.println("In save Test" + gameofthrones.getHeading()+"::");
System.out.println("In save Test" + gameofthrones.getParagraph()+"::");
System.out.println("In save Test" + gameofthrones.getImage()+"::");
gotService.savGot(gameofthrones);
List<Gameofthrones> alldata = gotService.getAllGots();
model.addAttribute("GameofthronesData", alldata);
return "home";
}
3.图像
解决方案
推荐阅读
- oracle - Oracle sqlloader,可以只在一列中插入结果吗?
- html - Bootstrap 4中心(水平)图像+ div内的文本
- java - 为什么我得到“唯一约束失败”错误
- java - 没有执行器的Java Callable async
- php - 如何对一个 JSON 对象进行字符串化,同时将另一个对象保持为整数
- amazon-web-services - 调用 CreateLoadBalancer 操作时出错 (InvalidSubnet):子网 ID 'MythicalMysfitsCoreStack:PublicSubnetOne' 无效
- python - 如何设置转弯(Python - Tkinter)?
- ubuntu - sage -i -> sage-run 收到未知选项:-i
- python - 使用距离过滤数据框
- r - 如何通过原始行 ID 重建标记化数据?