Spring 将 Blob 发送到存储过程
Spring Send Blob to Stored Procedure
我试图将 Blob 发送到存储过程但没有成功。
首先,我有一些限制,我。 e.我不能使用准备好的陈述。
其次,我确定存储过程可以正常工作,因为我已经成功地尝试过手动调用该过程并使用准备好的语句。
如果我执行此操作一切正常:
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();
但我不得不将 Spring MVC 与 WebSphere 应用程序服务器一起使用,我不能直接使用准备好的语句。
我必须使用 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 的 CustomStoredProcedure 中,我必须以这种方式关联 BLOB_NAME Oracle 数据类型:
public CustomStoredProcedure(DataSource dataSource, String sprocName) {
super(dataSource, sprocName);
declareParameter(new SqlParameter(BLOB_NAME, OracleTypes.BLOB));
compile();
}
现在,问题来了:
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 我没问题):
error: package com.ibm.websphere.rsadapter not exists
[javac] import com.ibm.websphere.rsadapter.WSCallHelper
(和之前一样,是我翻译的错误)
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 代码中,为简单起见,我删除了其他参数)。
PROCEDURE manda_mail( destinatario IN VARCHAR2,
cc IN VARCHAR2,
mittente IN VARCHAR2,
oggetto IN VARCHAR2,
corpo IN VARCHAR2 DEFAULT NULL,
nomeAllegato IN VARCHAR2 DEFAULT NULL,
allegato IN BLOB DEFAULT NULL)
AS
l_mail_conn UTL_SMTP.connection;
l_boundary VARCHAR2(50) := '----=*#abc1234321cba#*=';
l_step PLS_INTEGER := 12000; -- make sure you set a multiple of 3 not higher than 24573
v_mime VARCHAR2(50) := 'application/pdf';
BEGIN
l_mail_conn := UTL_SMTP.open_connection(C_SMTP_SERVER, 25);
UTL_SMTP.helo(l_mail_conn, C_SMTP_SERVER);
UTL_SMTP.mail(l_mail_conn, mittente);
UTL_SMTP.rcpt(l_mail_conn, destinatario);
UTL_SMTP.open_data(l_mail_conn);
UTL_SMTP.write_data(l_mail_conn, 'Date: ' || TO_CHAR(SYSDATE, 'DD-MON-YYYY HH24:MI:SS') || UTL_TCP.crlf);
UTL_SMTP.write_data(l_mail_conn, 'To: ' || destinatario || UTL_TCP.crlf);
UTL_SMTP.write_data(l_mail_conn, 'From: ' || mittente || UTL_TCP.crlf);
UTL_SMTP.write_data(l_mail_conn, 'Subject: ' || oggetto || UTL_TCP.crlf);
UTL_SMTP.write_data(l_mail_conn, 'Reply-To: ' || mittente || UTL_TCP.crlf);
UTL_SMTP.write_data(l_mail_conn, 'MIME-Version: 1.0' || UTL_TCP.crlf);
UTL_SMTP.write_data(l_mail_conn, 'Content-Type: multipart/mixed; boundary="' || l_boundary || '"' || UTL_TCP.crlf || UTL_TCP.crlf);
IF corpo IS NOT NULL THEN
UTL_SMTP.write_data(l_mail_conn, '--' || l_boundary || UTL_TCP.crlf);
UTL_SMTP.write_data(l_mail_conn, 'Content-Type: text/plain; charset="iso-8859-1"' || UTL_TCP.crlf || UTL_TCP.crlf);
UTL_SMTP.write_data(l_mail_conn, corpo);
UTL_SMTP.write_data(l_mail_conn, UTL_TCP.crlf || UTL_TCP.crlf);
END IF;
IF nomeAllegato IS NOT NULL THEN
UTL_SMTP.write_data(l_mail_conn, '--' || l_boundary || UTL_TCP.crlf);
UTL_SMTP.write_data(l_mail_conn, 'Content-Type: ' || v_mime || '; name="' || nomeAllegato || '"' || UTL_TCP.crlf);
UTL_SMTP.write_data(l_mail_conn, 'Content-Transfer-Encoding: base64' || UTL_TCP.crlf);
UTL_SMTP.write_data(l_mail_conn, 'Content-Disposition: attachment; filename="' || nomeAllegato || '"' || UTL_TCP.crlf || UTL_TCP.crlf);
FOR i IN 0 .. TRUNC((DBMS_LOB.getlength(allegato) - 1 )/l_step) LOOP
UTL_SMTP.write_data(l_mail_conn, UTL_RAW.cast_to_varchar2(UTL_ENCODE.base64_encode(DBMS_LOB.substr(allegato, l_step, i * l_step + 1))));
END LOOP;
UTL_SMTP.write_data(l_mail_conn, UTL_TCP.crlf || UTL_TCP.crlf);
END IF;
UTL_SMTP.write_data(l_mail_conn, '--' || l_boundary || '--' || UTL_TCP.crlf);
UTL_SMTP.close_data(l_mail_conn);
UTL_SMTP.quit(l_mail_conn);
END;
为此,我使用了以这种方式创建 blob 对象之前编写的代码:
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 发送到存储过程但没有成功。 首先,我有一些限制,我。 e.我不能使用准备好的陈述。 其次,我确定存储过程可以正常工作,因为我已经成功地尝试过手动调用该过程并使用准备好的语句。 如果我执行此操作一切正常:
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();
但我不得不将 Spring MVC 与 WebSphere 应用程序服务器一起使用,我不能直接使用准备好的语句。 我必须使用 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 的 CustomStoredProcedure 中,我必须以这种方式关联 BLOB_NAME Oracle 数据类型:
public CustomStoredProcedure(DataSource dataSource, String sprocName) {
super(dataSource, sprocName);
declareParameter(new SqlParameter(BLOB_NAME, OracleTypes.BLOB));
compile();
}
现在,问题来了:
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 我没问题):
error: package com.ibm.websphere.rsadapter not exists
[javac] import com.ibm.websphere.rsadapter.WSCallHelper
(和之前一样,是我翻译的错误)
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 代码中,为简单起见,我删除了其他参数)。
PROCEDURE manda_mail( destinatario IN VARCHAR2,
cc IN VARCHAR2,
mittente IN VARCHAR2,
oggetto IN VARCHAR2,
corpo IN VARCHAR2 DEFAULT NULL,
nomeAllegato IN VARCHAR2 DEFAULT NULL,
allegato IN BLOB DEFAULT NULL)
AS
l_mail_conn UTL_SMTP.connection;
l_boundary VARCHAR2(50) := '----=*#abc1234321cba#*=';
l_step PLS_INTEGER := 12000; -- make sure you set a multiple of 3 not higher than 24573
v_mime VARCHAR2(50) := 'application/pdf';
BEGIN
l_mail_conn := UTL_SMTP.open_connection(C_SMTP_SERVER, 25);
UTL_SMTP.helo(l_mail_conn, C_SMTP_SERVER);
UTL_SMTP.mail(l_mail_conn, mittente);
UTL_SMTP.rcpt(l_mail_conn, destinatario);
UTL_SMTP.open_data(l_mail_conn);
UTL_SMTP.write_data(l_mail_conn, 'Date: ' || TO_CHAR(SYSDATE, 'DD-MON-YYYY HH24:MI:SS') || UTL_TCP.crlf);
UTL_SMTP.write_data(l_mail_conn, 'To: ' || destinatario || UTL_TCP.crlf);
UTL_SMTP.write_data(l_mail_conn, 'From: ' || mittente || UTL_TCP.crlf);
UTL_SMTP.write_data(l_mail_conn, 'Subject: ' || oggetto || UTL_TCP.crlf);
UTL_SMTP.write_data(l_mail_conn, 'Reply-To: ' || mittente || UTL_TCP.crlf);
UTL_SMTP.write_data(l_mail_conn, 'MIME-Version: 1.0' || UTL_TCP.crlf);
UTL_SMTP.write_data(l_mail_conn, 'Content-Type: multipart/mixed; boundary="' || l_boundary || '"' || UTL_TCP.crlf || UTL_TCP.crlf);
IF corpo IS NOT NULL THEN
UTL_SMTP.write_data(l_mail_conn, '--' || l_boundary || UTL_TCP.crlf);
UTL_SMTP.write_data(l_mail_conn, 'Content-Type: text/plain; charset="iso-8859-1"' || UTL_TCP.crlf || UTL_TCP.crlf);
UTL_SMTP.write_data(l_mail_conn, corpo);
UTL_SMTP.write_data(l_mail_conn, UTL_TCP.crlf || UTL_TCP.crlf);
END IF;
IF nomeAllegato IS NOT NULL THEN
UTL_SMTP.write_data(l_mail_conn, '--' || l_boundary || UTL_TCP.crlf);
UTL_SMTP.write_data(l_mail_conn, 'Content-Type: ' || v_mime || '; name="' || nomeAllegato || '"' || UTL_TCP.crlf);
UTL_SMTP.write_data(l_mail_conn, 'Content-Transfer-Encoding: base64' || UTL_TCP.crlf);
UTL_SMTP.write_data(l_mail_conn, 'Content-Disposition: attachment; filename="' || nomeAllegato || '"' || UTL_TCP.crlf || UTL_TCP.crlf);
FOR i IN 0 .. TRUNC((DBMS_LOB.getlength(allegato) - 1 )/l_step) LOOP
UTL_SMTP.write_data(l_mail_conn, UTL_RAW.cast_to_varchar2(UTL_ENCODE.base64_encode(DBMS_LOB.substr(allegato, l_step, i * l_step + 1))));
END LOOP;
UTL_SMTP.write_data(l_mail_conn, UTL_TCP.crlf || UTL_TCP.crlf);
END IF;
UTL_SMTP.write_data(l_mail_conn, '--' || l_boundary || '--' || UTL_TCP.crlf);
UTL_SMTP.close_data(l_mail_conn);
UTL_SMTP.quit(l_mail_conn);
END;
为此,我使用了以这种方式创建 blob 对象之前编写的代码:
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 绑定。 行得通。