从存储过程调用流化blob数据

时间:2016-08-25 10:36:22

标签: java spring oracle

我正在尝试流式传输从存储过程调用返回的blob。我试图使用spring AbstractLobStreamingResultSetExtractor。

public OutputStream getDocument(final Document doc, final OutputStream outStream) { 

    SimpleJdbcCall simpleJdbcCall = new SimpleJdbcCall(rdcJdbcTemplate)
    .withProcedureName(PROC_NAME)
    .withCatalogName(CATALOG_NAME)
    .withSchemaName(SCHEMA_NAME).declareParameters(new SqlParameter(DOC_ID_PARAM, OracleTypes.VARCHAR),
            new SqlOutParameter(OUT_PARAM, OracleTypes.BLOB , new AbstractLobStreamingResultSetExtractor<Object>(){

                @Override
                protected void streamData(ResultSet rs) throws SQLException,
                        IOException, DataAccessException {

                    InputStream blobStream = lobHandler.getBlobAsBinaryStream(rs, 1);               
                    if (blobStream != null){                    
                        FileCopyUtils.copy(blobStream, outStream);  
                    }       
                }
            }));

    SqlParameterSource in = new MapSqlParameterSource().addValue(DOC_ID_PARAM, doc.getId());

    Map<String,Object> out = simpleJdbcCall.execute(in);
    return outStream;
}

当我调试这个时,从不调用streamdata代码。

关于如何实现这一点的任何想法?

1 个答案:

答案 0 :(得分:0)

仅为未来的读者找到了解决方案

public OutputStream getGlasDocument(final Document doc, final OutputStream outStream) {             

    SimpleJdbcCall simpleJdbcCall = new SimpleJdbcCall(rdcJdbcTemplate)
    .withProcedureName(PROC_NAME)
    .withCatalogName(CATALOG_NAME)
    .withSchemaName(SCHEMA_NAME).declareParameters(new SqlParameter(DOC_ID_PARAM, OracleTypes.VARCHAR),
            new SqlOutParameter(OUT_PARAM, OracleTypes.BLOB, null, new SqlReturnType() {

                @Override
                public Object getTypeValue(CallableStatement cs, int paramIndex,
                        int sqlType, String typeName) throws SQLException {
                    try {
                        Blob blob = cs.getBlob(1);
                        if (blob != null){
                            FileCopyUtils.copy(cs.getBlob(1).getBinaryStream(), outStream);
                        }                           
                    } catch (IOException e) {                           
                        logger.debug(e.getMessage());
                    }
                    return null;
                }
            }));

    SqlParameterSource in = new MapSqlParameterSource().addValue(DOC_ID_PARAM, doc.getId());        
    simpleJdbcCall.execute(in);
    return outStream;
}