2017-03-04 95 views
1

我想通过下面的代码插入BLOB与Spring的JdbcTemplate

LobHandler handler = new DefaultLobHandler(); 
int dbresponse = jdbcTemplate.update(DBConstants.INSERT_INVOICE, new Object[]{invoiceBean.getVendorid(), 
     new SqlLobValue(invoiceBean.getInvoiceImage(), invoiceBean.getInvoiveImageLength(), handler), invoiceBean.getInvoiceDate()}, 
     Types.INTEGER,Types.BLOB, Types.VARCHAR); 

但得到以下错误

Caused by: java.sql.SQLException: Invalid argument value: java.io.NotSerializableException 

....... 

    Caused by: java.io.NotSerializableException: org.springframework.jdbc.core.support.SqlLobValue 

我做了invoiceBean类为Serializable,但得到同样的错误插入使用的JdbcTemplate在发表blob 。

注:小尺寸的图像被成功地插入到数据库中,但问题带有大图像大小通常大于1 MB

敬请咨询!

+0

在你的控制器,你应该确定在使用多对象的getSize方法对应的文件的字节大小。那就是你在getInvoiveImageLength方法 – artemisian

+0

中应该返回的值是否让它工作? – artemisian

+0

由于时间紧迫,我暂时切换为将图像上传到FileSystem。我会研究你的建议并让你知道。 – Ankit

回答

0

我觉得最后一个参数应该是一个int数组:

LobHandler handler = new DefaultLobHandler(); 
int dbresponse = jdbcTemplate.update(
         DBConstants.INSERT_INVOICE, 
         new Object[]{ 
           invoiceBean.getVendorid(), 
           new SqlLobValue(invoiceBean.getInvoiceImage(), invoiceBean.getInvoiveImageLength(), handler), 
           invoiceBean.getInvoiceDate()}, 
         new int[] {Types.INTEGER, Types.BLOB, Types.VARCHAR}); 
+0

现在,出现此错误:导致:com.mysql.jdbc.PacketTooBigException:用于查询的数据包太大(1559540> 1048576)。 – Ankit

+0

你可以显示你的getInvoiceImage代码吗? – artemisian

+0

它是一个返回InputStream对象的getter方法。实际上,流程是我在控制器上获取Multipart对象,并将输入流对象从multipart设置为invoicebean类(其中包含inputstream类型的invoiceImage) – Ankit