Java 从multipartFile转换为Blob失败
我想将文档保存到MySQL数据库 我有以下控制器Java 从multipartFile转换为Blob失败,java,spring,thymeleaf,Java,Spring,Thymeleaf,我想将文档保存到MySQL数据库 我有以下控制器 @RequestMapping(value = "/save", method = RequestMethod.POST) public String save( @ModelAttribute("document") @Valid Document document, BindingResult bindingResult, @RequestParam("cont
@RequestMapping(value = "/save", method = RequestMethod.POST)
public String save(
@ModelAttribute("document") @Valid Document document,
BindingResult bindingResult,
@RequestParam("content") MultipartFile file) {
及表格:
<form th:action="@{/save}" th:object="${document}" method="post" enctype="multipart/form-data">
<h4>Nazwa</h4>
<input type="text" th:field="*{name}"/>
<td style="color:red" th:if="${#fields.hasErrors('name')}" th:errors="*{name}"></td>
<h4>Opis</h4>
<input type="text" th:field="*{description}"/>
<td style="color:red" th:if="${#fields.hasErrors('description')}" th:errors="*{description}"></td>
<h4>zawartosc</h4>
<input type="file" th:field="*{content}"/>
<td style="color:red" th:if="${#fields.hasErrors('content')}" th:errors="*{content}"></td>
纳兹瓦
奥皮斯
扎瓦托斯
我不知道如何修复这个错误:
无法转换类型的属性值
org.springframework.web.multipart.support.StandardMultipartTTPServletRequest$StandardMultipartFile
属性内容必须输入java.sql.Blob;嵌套异常
is java.lang.IllegalStateException:无法转换类型的值
[org.springframework.web.multipart.support.StandardMultipartTTPServletRequest$StandardMultipartFile]
属性内容的必需类型[java.sql.Blob]:不匹配
找到编辑器或转换策略
似乎您正在尝试使用file变量来持久化 如果正确获取文件,请将
file.getBytes()
设置到blob字段中,您应该能够将其持久化