将Multipart文件插入数据库

时间:2014-02-15 09:46:02

标签: java spring hibernate jpa multipart

我正在尝试使用以下代码

更新Oracle Db中的文件
@RequestMapping(value = "/person/{personId}", method = RequestMethod.POST)
    public @ResponseBody String save(@RequestParam MultipartFile files,@PathVariable int personId,) throws IOException {

        byte [] bFile =files.getBytes();
        personService.uploadImageOnId(personId,bFile);
}

这是ServiceImpl

@Override
public void uploadImageOnId(int personId,byte [] personImage) {
        entityManager.createNamedQuery("Person.uploadImageOnId")
                .setParameter("personId", personId).setParameter("personImage", personImage)
                .executeUpdate();

    }

实体

@Entity
@Table(name = "PERSON")
@NamedQueries({ 
@NamedQuery(name = "Person.uploadImageOnId", query = "UPDATE Person p SET p.personImage= :personImage WHERE p.personId= :personId" )

})
public class Person implements java.io.Serializable {
    @Column(name="PERSON_IMAGE")
        @Lob
        private byte[] personImage;

        public byte[] getPersonImage() {
            return personImage;
        }

        public void setPersonImage(byte[] personImage) {
            this.personImage = personImage;
        }
}

但是我遇到了以下错误

java.lang.ClassCastException: [B cannot be cast to [Ljava.lang.Object;

请指出我做错的地方。

1 个答案:

答案 0 :(得分:2)

参考Inserting byte[] array as blob in Oracle Database getting ORA-01460: unimplemented or unreasonable conversion requested,我建议不要将字节数组设置为值,而是将ByteArrayInputStream包装成数组。