2017-03-04 66 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