Java Spring将Blob发送到存储过程

Java Spring将Blob发送到存储过程,java,spring,stored-procedures,websphere,blob,Java,Spring,Stored Procedures,Websphere,Blob,我试图向存储过程发送一个Blob,但没有成功。 首先,我有一些局限性。E我不能用事先准备好的陈述。 第二,我确信存储过程是有效的,因为我已经尝试了手动调用该过程并使用prepared语句成功。 如果我执行此操作,所有操作都正常: PreparedStatement ps = con.prepareStatement("call t1000_adm.Mandamail.manda_mail(?)"); file = new File(request.getPathAllegato()); Inpu

我试图向存储过程发送一个Blob,但没有成功。 首先,我有一些局限性。E我不能用事先准备好的陈述。 第二,我确信存储过程是有效的,因为我已经尝试了手动调用该过程并使用prepared语句成功。 如果我执行此操作,所有操作都正常:

PreparedStatement ps = con.prepareStatement("call t1000_adm.Mandamail.manda_mail(?)");
file = new File(request.getPathAllegato());
InputStream fis = new FileInputStream(file);
ps.setBinaryStream(1, fis, (int)file.length());
ps.execute();
但是我不得不将SpringMVC与WebSphereApplicationServer一起使用,我不能直接使用准备好的语句。 我必须使用javax.sql.DataSource和org.springframework.jdbc.core.namedparam.MapSqlParameterSource

这就是我要做的:

MapSqlParameterSource paramSource = new MapSqlParameterSource();
paramSource.addValue(BLOB_NAME, blob);
CustomStoredProcedure proc = new CustomStoredProcedure( dataSource, storedProcedure); 
proc.execute(paramSource);
在扩展org.springframework.jdbc.object.StoredProcedure的CustomStoredProcess中,我必须将一个Oracle数据类型与BLOB_NAME关联,如下所示:

public CustomStoredProcedure(DataSource dataSource, String sprocName) {
    super(dataSource, sprocName);
    declareParameter(new SqlParameter(BLOB_NAME, OracleTypes.BLOB));
    compile();
}
BLOB blob = BLOB.createTemporary(WSCallHelper.getNativeConnection(dataSource.getConnection()), false, BLOB.DURATION_SESSION);
FileInputStream is = new FileInputStream(filePath);
byte[] buffer = new byte[30000000];
ByteArrayOutputStream baos = new ByteArrayOutputStream();

int bytesRead;
while ((bytesRead = is.read(buffer)) != -1) {
    baos.write(buffer, 0, bytesRead);
}

blob.setBytes(1, baos.toByteArray());       
现在,问题是:

1) 如果尝试使用dataSource.getConnection().createBlob()创建blob对象,则会收到错误:

java.sql.SQLFeatureNotSupportedException: DSRA1300E: Function not implemented: Connection.createBlob 
(描述可能不正确,但我有本地化版本,并翻译成英语)

2) 如果我尝试使用blob.createTemporary创建blob对象,我将收到错误消息

java.lang.ClassCastException: com.ibm.ws.rsadapter.jdbc.WSJdbcConnection incompatible with oracle.jdbc.OracleConnection
3) 如果我使用

com.ibm.websphere.rsadapter.WSCallHelper.getNativeConnection( dataSource.getConnection)
我收到编译错误(仅使用Ant,使用Eclipse我没有问题):

(和以前一样,我翻译了错误)

4) 如果忽略Ant编译错误并使用它,我将收到执行错误:

[31/05/17 9.52.50:414 CEST] 0000007f SystemErr     R org.springframework.jdbc.UncategorizedSQLException: CallableStatementCallback; uncategorized SQLException for SQL [{call MANDAMAIL.MANDA_MAIL(?)}]; SQL state [99999]; error code [22922]; ORA-22922: valore LOB non esistente
ORA-06512: a "SYS.DBMS_LOB", line 837
ORA-06512: a "T1000_ADM.MANDAMAIL", line 190
ORA-06512: a line 1
; nested exception is java.sql.SQLException: ORA-22922: valore LOB non esistente
ORA-06512: a "SYS.DBMS_LOB", line 837
ORA-06512: a "T1000_ADM.MANDAMAIL", line 190
ORA-06512: a line 1

[31/05/17 9.52.50:415 CEST] 0000007f SystemErr     R    at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:83)
[31/05/17 9.52.50:415 CEST] 0000007f SystemErr     R    at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:80)
[31/05/17 9.52.50:416 CEST] 0000007f SystemErr     R    at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:80)
[31/05/17 9.52.50:416 CEST] 0000007f SystemErr     R    at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:1030)
[31/05/17 9.52.50:416 CEST] 0000007f SystemErr     R    at org.springframework.jdbc.core.JdbcTemplate.call(JdbcTemplate.java:1064)
[31/05/17 9.52.50:417 CEST] 0000007f SystemErr     R    at org.springframework.jdbc.object.StoredProcedure.execute(StoredProcedure.java:144)
[31/05/17 9.52.50:417 CEST] 0000007f SystemErr     R    at com.bpvn.vieniconnoi.dao.MandaMailStoredProcedure.execute(MandaMailStoredProcedure.java:42)...
该过程的第190行是DBMS_LOB的第一次出现。 这是存储过程(在Java代码中,为了简单起见,我删除了其他参数)

为了实现这一点,我使用了在创建blob对象之前编写的代码:

public CustomStoredProcedure(DataSource dataSource, String sprocName) {
    super(dataSource, sprocName);
    declareParameter(new SqlParameter(BLOB_NAME, OracleTypes.BLOB));
    compile();
}
BLOB blob = BLOB.createTemporary(WSCallHelper.getNativeConnection(dataSource.getConnection()), false, BLOB.DURATION_SESSION);
FileInputStream is = new FileInputStream(filePath);
byte[] buffer = new byte[30000000];
ByteArrayOutputStream baos = new ByteArrayOutputStream();

int bytesRead;
while ((bytesRead = is.read(buffer)) != -1) {
    baos.write(buffer, 0, bytesRead);
}

blob.setBytes(1, baos.toByteArray());       
有什么建议吗?

问题已解决: 我不需要使用Blob对象:我可以在Java端使用简单的byte[],将其放入paramSource中,并用OracleTypes.BINARY绑定该数组。 有效。

问题已解决: 我不需要使用Blob对象:我可以在Java端使用简单的byte[],将其放入paramSource中,并用OracleTypes.BINARY绑定该数组。 这很有效