title: How to upload and save image in MySQL via SpringBoot JPA
body: <p>I'm trying to upload and save an image in MySQL with SpringBoot using JPA but unfortunately I'm greeted with this error, can anyone tell me what am I missing ?</p>
  <p>Error</p>
  <pre><code>    Field error in object 'institution' on field 'logo': rejected value [org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile@387c2996]; codes [typeMismatch.institution.logo,typeMismatch.logo,typeMismatch.[Ljava.lang.Byte;,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [institution.logo,logo]; arguments []; default message [logo]]; default message [Failed to convert property value of type 'org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile' to required type 'java.lang.Byte[]' for property 'logo'; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type 'org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile' to required type 'java.lang.Byte' for property 'logo[0]': PropertyEditor [org.springframework.beans.propertyeditors.CustomNumberEditor] returned inappropriate value of type 'org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile']]
  </code></pre>
  <p>pom.xml</p>
  <pre><code>    &lt;dependency&gt;
          &lt;groupId&gt;commons-fileupload&lt;/groupId&gt;
          &lt;artifactId&gt;commons-fileupload&lt;/artifactId&gt;
          &lt;version&gt;1.4&lt;/version&gt;
      &lt;/dependency&gt;
  </code></pre>
  <p>Entity</p>
  <pre><code>    @Lob
      private Byte[] logo; 
      
  </code></pre>
  <p>controller</p>
  <pre><code>    @PostMapping(&quot;/institutionAddNew&quot;)
      public String addNewComp(@ModelAttribute(&quot;institution&quot;) Institution institution, Model model, 
               HttpSession session,
              @RequestParam CommonsMultipartFile fileUpload) throws Exception {
            
          Integer insId = (Integer) session.getAttribute(&quot;instId&quot;);
          
          Byte[] byteObjects = new Byte[fileUpload.getBytes().length];
          int i = 0;
          for (byte b : fileUpload.getBytes()){
              byteObjects[i++] = b;
          }
          institution.setLogo(byteObjects);
          
          cs.addNewInstitution(institution);
          model.addAttribute(&quot;institutionlist&quot;, cs.getAllInstitutions(insId));
          return &quot;redirect:/showAllInstitutions&quot;;
  }   
  </code></pre>
  <p>Form</p>
  <pre><code>    &lt;form:form action=&quot;/institutionAddNew&quot; method=&quot;post&quot; modelAttribute=&quot;institution&quot;  enctype=&quot;multipart/form-data&quot;&gt;
          &lt;form:input path=&quot;instName&quot; /&gt;
          &lt;input type=&quot;file&quot; name=&quot;logo&quot; /&gt;
  
          &lt;button type=&quot;submit&quot; class=&quot;btn btn-primary&quot;&gt;Submit&lt;/button&gt;
      &lt;/form:form&gt;
  </code></pre>