首页 > 解决方案 > 使用 Spring MVC 从数据库存储和检索图像时图像损坏

问题描述

我在从 MySQL 数据库存储和检索图像时遇到问题。我可以通过以下方式将图像保存在 MySQL 数据库中。

JSP Page(Multipart Object) -> Service 类中的 byte[] -> DAO 类中的 SerialBlob。

控制器片段:

    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public String productsPageInsert(Map<String, Object> map, @Valid 
    @ModelAttribute("productCmd") ProductsCommand productsCommand, 
    BindingResult errors) throws IOException {

      List<ProductLinksDTO> listLinksDTO = null;
      ProductLinksDTO linkDTO1 = null, linkDTO2 = null;
      ProductsDTO productsDTO = null;
      List<ProductsDTO> listDTO = null;
      String result = null;

      try {
        if (errors.hasErrors()) {
            return "adminProductsDef";
        }
        listLinksDTO = new ArrayList<>();
        listDTO = new ArrayList<>();
        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";
        }
   }

服务类片段

@Override
@Transactional
public String addProduct(ProductsDTO productsDTO) throws SerialException, SQLException {
    int count = 0;
    ProductsHLO productsHLO = null;

    List<ProductLinksHLO> listLinkHLO = new ArrayList<>();
    productsHLO = new ProductsHLO();

    // copy dto data into hlo

        productsDTO.getLinks().forEach(linkDTO -> {
        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!";
}

请帮助我并给我任何建议,这样就不会发生这种情况。如果您对此操作有任何其他想法,请告诉我。

谢谢。

标签: javaspring-mvc

解决方案


推荐阅读