java - 使用 Spring MVC 从数据库存储和检索图像时图像损坏
问题描述
我在从 MySQL 数据库存储和检索图像时遇到问题。我可以通过以下方式将图像保存在 MySQL 数据库中。
JSP Page(Multipart Object) -> Service 类中的 byte[] -> DAO 类中的 SerialBlob。
从使用 EL 和 SpEL 的 JSP 页面中,我以 Multipart 对象的形式传递图像文件,然后在控制器中将multipart 转换为 byte[] 并将其存储在 DTO 中,然后通过将其转换为Service 类中的SerialBLOB来传递该 byte[] 和使用 DAO 将其存储到数据库中。它正在有效地工作。
为了从数据库中检索图像,我首先将检索到的图像以 BLOB 对象的形式存储,然后将其转换为 byte[],但是要将图像查看到 JSP 中,我必须将 byte[]] 转换为 String 格式。当我创建了两个不同的项目来存储和检索图像时,它就起作用了。
但是,当我尝试从数据库存储和检索图像时出现问题,因为我必须将 DTO 的图像属性返回类型更改为字符串,因为我必须将字符串传递到我的 JSP 页面才能查看图像。在这里,检索工作正常,但是当我尝试在将多部分转换为字符串并将字符串转换为(字符串->字节[]->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";
}
}
- 当我将 Multipart 对象文件转换为 String 时,它必须被损坏。
服务类片段
@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!";
}
请帮助我并给我任何建议,这样就不会发生这种情况。如果您对此操作有任何其他想法,请告诉我。
谢谢。
解决方案
推荐阅读
- python - 编码按钮 Yandex POST 数据
- rust - 如何在编译期间从 Rust 中的字符串文字中删除空格?
- flask - 谷歌云运行突然流量峰值错误
- c++ - 卡在正确地从 txt 文件中读取凭据
- javascript - 获取 shadow DOM 的 document.body/html
- android - 如何将撰写预览导出为 png/jpg
- prestashop - prestashop 1.7.8.0 polyfill-intl-normalizer 中的错误更新
- python - 怎么解决
Python中的错误? - javascript - 解构 JavaScript 对象
- mysql - Python SQL:断言 isinstance(query, (bytes, bytearray)) AssertionError