2016-12-31 321 views
0

我想將文檔保存到MySQL數據庫。無法從multipartFile轉換爲Blob

我有以下控制器

@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> 

,我沒有知道如何解決這個錯誤:

Failed to convert property value of type org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile to required type java.sql.Blob for property content; nested exception is java.lang.IllegalStateException: Cannot convert value of type [org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile] to required type [java.sql.Blob] for property content: no matching editors or conversion strategy found

回答

0

看來你是試圖使用文件變量來堅持。

如果您正確獲取文件,請將file.getBytes()設置到您的blob字段中,並且您應該可以保留它。

相關問題