title: Image get corrupted while storing and retrieving it from database using Spring MVC
body: <p>I am getting problem while storing and retrieving image from MySQL database. I am able to save the image in MySQL database in following manner.</p>
  
  <p>JSP Page(Multipart Object) -> byte[] in Service class -> SerialBlob in DAO class.</p>
  
  <ul>
  <li><p>From JSP page using EL and SpEL I passed image file in the form of Multipart object, and then in controller converted <b>multipart into byte[] </b>  and stored it inside the DTO and then pass that byte[] by converting it into <b>SerialBLOB </b> in Service class and stored it into database using DAO. 
  It is working efficiently.</p></li>
  <li><p>To retrieve image from database, first I stored the retrieved image in the the form of BLOB object and then converted it into byte[], but to view the image into JSP I had to convert byte[]] into String format. It is working when I have created two different projects for storing and retrieving images.</p></li>
  <li><p>But, the problem came when I tried to do both storing and retrieving image from database, for that I had to change my DTO's image property return type into String because I had to pass String into my JSP page to view image. Here, retrieving worked fine but when I tried to store the image file when converting multipart into string and string into ( string -> byte[ ] -> SerialBLOB ) SerialBLOB it stored into the database but the image get corrupted, it say's <b>invalid image</b> when open.</p></li>
  </ul>
  
  <h2>Controller Snippet:</h2>
  
  <pre><code>    @RequestMapping(value = "/insert", method = RequestMethod.POST)
      public String productsPageInsert(Map&lt;String, Object&gt; map, @Valid 
      @ModelAttribute("productCmd") ProductsCommand productsCommand, 
      BindingResult errors) throws IOException {
  
        List&lt;ProductLinksDTO&gt; listLinksDTO = null;
        ProductLinksDTO linkDTO1 = null, linkDTO2 = null;
        ProductsDTO productsDTO = null;
        List&lt;ProductsDTO&gt; listDTO = null;
        String result = null;
  
        try {
          if (errors.hasErrors()) {
              return "adminProductsDef";
          }
          listLinksDTO = new ArrayList&lt;&gt;();
          listDTO = new ArrayList&lt;&gt;();
          linkDTO1 = new ProductLinksDTO();
          linkDTO2 = new ProductLinksDTO();
          productsDTO = new ProductsDTO();
  
          // Adding links to list
          linkDTO1.setLink(productsCommand.getLinkFirst());
          linkDTO2.setLink(productsCommand.getLinkSecond());
          listLinksDTO.add(linkDTO1);
          listLinksDTO.add(linkDTO2);
  
          // Adding data into dto
          productsDTO.setProductName(productsCommand.getProductName());
          productsDTO.setDescription(productsCommand.getDescription());
          productsDTO.setLinks(listLinksDTO);
  
          // adding image into products dto.
          productsDTO.setImage(new String(productsCommand.getImage().getBytes()));
  
          listDTO.add(productsDTO);
  
          result = productsService.addProduct(productsDTO);
          map.put("resultMsg", result);
          map.put("products", listDTO);
  
          return "redirect: /home_page/products_page.htm";
          } catch (DataAccessException dae) {
          map.put("errorMsg", dae.getMessage() + "\n" + dae.getStackTrace());
          return "failure";
          } catch (SerialException e) {
          map.put("errorMsg", e.getMessage() + "\n" + e.getStackTrace());
          return "failure";
          } catch (SQLException e) {
          map.put("errorMsg", e.getMessage() + "\n" + e.getStackTrace());
          return "failure";
          } catch (Exception e) {
          map.put("errorMsg", e.getMessage() + "\n" + e.getStackTrace());
          return "failure";
          }
     }
  </code></pre>
  
  <ul>
  <li>When I was converting Multipart object file into String it must get corrupted. </li>
  </ul>
  
  <h2>Service class snippet</h2>
  
  <pre><code>@Override
  @Transactional
  public String addProduct(ProductsDTO productsDTO) throws SerialException, SQLException {
      int count = 0;
      ProductsHLO productsHLO = null;
  
      List&lt;ProductLinksHLO&gt; listLinkHLO = new ArrayList&lt;&gt;();
      productsHLO = new ProductsHLO();
  
      // copy dto data into hlo
  
          productsDTO.getLinks().forEach(linkDTO -&gt; {
          ProductLinksHLO linkHLO = new ProductLinksHLO();
          BeanUtils.copyProperties(linkDTO, linkHLO);
          listLinkHLO.add(linkHLO);
      });
  
      productsHLO.setDescription(productsDTO.getDescription());
  
      // converting (image) String into SerialBlob  
      productsHLO.setImage(new SerialBlob(productsDTO.getImage().getBytes()));
  
      productsHLO.setProductName(productsDTO.getProductName());
      productsHLO.setLinks(listLinkHLO);
  
      // executing dao method
      count = productsDAO.insertProduct(productsHLO);
      if (count != 0)
          return "Product added successfully";
      else
          return "Product Not added!";
  }
  </code></pre>
  
  <p>Please help me and give me any suggestion so this won't happen. If you have any other idea for this operation then please tell me.</p>
  
  <p>Thanks.</p>