Spring将Blob发送到存储过程

时间:2017-05-31 08:31:13

标签: java spring stored-procedures websphere blob

我试图将Blob发送到存储过程但没有成功。 首先,我有一些限制,我。即我不能使用准备好的声明。 其次,我确信存储过程有效,因为我已经成功调用了手动程序和准备好的语句。 如果我执行这一切都正常工作:

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);

在CustomStoredProcedure中,我扩展了org.springframework.jdbc.object.StoredProcedure,我必须以这种方式将BLOB_NAME与Oracle数据类型关联起来:

public CustomStoredProcedure(DataSource dataSource, String sprocName) {
    super(dataSource, sprocName);
    declareParameter(new SqlParameter(BLOB_NAME, OracleTypes.BLOB));
    compile();
}

现在,问题在于:

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

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());       

有什么建议吗?

1 个答案:

答案 0 :(得分:2)

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