sql—如何使用JavaSpring在数据库中保存和显示图像

nxagd54h  于 2021-06-27  发布在  Java
关注(0)|答案(1)|浏览(212)

我试图保存在数据库图像,我添加到我的数据库字段图像与数据类型longblob,现在我想办法保存它。对于我的html文件,我有输入类型文件,当我试图保存时,我有这样的错误。

Field error in object 'item' on field 'image': rejected value [thumb-252577.png]; codes [typeMismatch.item.image,typeMismatch.image,typeMismatch.[Ljava.lang.Byte;,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [item.image,image]; arguments []; default message [image]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.lang.Byte[]' for property 'image'; nested exception is java.lang.NumberFormatException: For input string: "thumb-252577.png"]]

我有object item.java和

private Byte[] image;

我的控制器看起来像这样

@PostMapping(value = "/items/add")
    public String itemAddPost(Model model, HttpSession session, @ModelAttribute("item") item item, ){
        User isExist= (User) session.getAttribute("loggedUser");
        if (null == isExist){
            return "index";
        }
        item.setWhoAdd(isExist.getId());
        System.out.println(item.getImage());
        itemRepository.save(item);
        return "redirect:/items";
    }
u2nhd7ah

u2nhd7ah1#

当我正确地理解了你发送的错误信息时 thumb-252577.png 作为字符串发送到后端,解析器显然无法将其转换为字节数组。所以你必须检查你发送给你的spring控制器的数据。

相关问题