【问题标题】:storing image into database by using SpringMVC and Hibernate使用 Spring MVC 和 Hibernate 将图像存储到数据库中
【发布时间】:2014-09-04 12:05:51
【问题描述】:

以下是我尝试使用springMVCHiberrnate 将图像存储到数据库时的错误消息。

无法将“org.springframework.web.multipart.commons.CommonsMultipartFile”类型的属性值转换为属性所需的“java.sql.Blob”类型

我正在使用以下代码集:-

1)在jsp中

<html><body>
<form:form  action="submitPartner1.do" commandName="partnerindividual"  enctype="multipart/form-  data" >
 <input type="file" name="image" />
<form:form>
</body>
</html>

2)Partner.java

class Partner{

 Blob image;

 //getter and setter

 }

3)在控制器类中

@RequestMapping(value="/submitPartner1.do",method=RequestMethod.POST)

 public String save(@ModelAttribute("partnerindividual")Partner partnerindividual,@RequestParam("file") MultipartFile file) throws IOException{

    Blob blob=null;
        try{
       byte[] contents = file.getBytes();
     blob = new SerialBlob(contents);
          partnerindividual.setImage(blob);
        //storing into database
        service.save(partnerindividual);
       }
       catch(Exception e){e.printStackTrace();}
       System.out.println(partindi);

return "becomeapartnerContinue";

}
}

4) 在spring_config.xml

    <bean id="multipartResolver"
    class="org.springframework.web.multipart.commons.CommonsMultipartResolver"
    <property name="maxUploadSize" value="10000000" />
    </bean>

【问题讨论】:

    标签: hibernate spring-mvc


    【解决方案1】:

    错误表明从文件字节到 Blob 的转换存在问题。

    你能在控制器中更改你的 save() 方法吗?

    替换下面的代码行

    blob = new SerialBlob(contents);
    
            with
    
    blob = Hibernate.createBlob(contents);
    

    【讨论】:

    • 什么是session对象,不管是HttpSession还是HibernateSession对象,都在这里......
    • 感谢指点,createBlob(byte[]) 属于Hibernate类,基本上是final类。
    • 它仍然会导致同样的问题 java.lang.IllegalStateException: Cannot convert value of type [org.springframework.web.multipart.commons.CommonsMultipartFile] to required type [java.sql.Blob] for property “图片”:未找到匹配的编辑器或转换策略]
    猜你喜欢
    • 2012-05-27
    • 2014-08-25
    • 2018-09-25
    • 2015-07-16
    • 1970-01-01
    • 2015-01-04
    • 2016-09-22
    • 1970-01-01
    • 2010-12-27
    相关资源
    最近更新 更多