0

我有以下代码:

 public synchronized InputStream getResourceStream(final String name)
        throws ResourceNotFoundException
    {
        if (org.apache.commons.lang.StringUtils.isEmpty(name))
        {
            throw new ResourceNotFoundException("DataSourceResourceLoader: Template name was empty or null");
        }

        Connection conn = null;
        ResultSet rs = null;
        PreparedStatement ps = null;
        try
        {
            conn = openDbConnection();
            ps = getStatement(conn, templateColumn, name);
            rs = ps.executeQuery();

            if (rs.next())
            {
                InputStream stream = rs.getBinaryStream(templateColumn);
                if (stream == null)
                {
                    throw new ResourceNotFoundException("DataSourceResourceLoader: "
                                                        + "template column for '"
                                                        + name + "' is null");
                }

                return new BufferedInputStream(stream);
            }
            else
            {
                throw new ResourceNotFoundException("DataSourceResourceLoader: "
                                                    + "could not find resource '"
                                                    + name + "'");

            }
        }
        catch (SQLException sqle)
        {
            String msg = "DataSourceResourceLoader: database problem while getting resource '"
                         + name + "': ";

            log.error(msg, sqle);
            throw new ResourceNotFoundException(msg);
        }
        catch (NamingException ne)
        {
            String msg = "DataSourceResourceLoader: database problem while getting resource '"
                         + name + "': ";

            log.error(msg, ne);
            throw new ResourceNotFoundException(msg);
        }
        finally
        {
            closeResultSet(rs);
            closeStatement(ps);
            closeDbConnection(conn);
        }
    }

上面的方法返回类型是InputStream. 在上面我得到列值 InputStream stream = rs.getBinaryStream(templateColumn);并返回相同的值。现在我的要求是我必须再检索一列的值并与templateColumn. 我怎样才能做到这一点?

基本上在上述逻辑中,我必须再添加一行,如下所示。

InputStream stream2 = rs.getBinaryStream(oneMoreColumn);

现在是否可以在单个 Stream 中返回两个值?

谢谢!

4

1 回答 1

0

您可以将两个流完全读入 abyte[]并返回一个 new ByteArrayInputStream,但这没有任何实际意义,因为客户端代码不知道数组的哪一部分是您的第一个流,哪一部分是第二个流。

而是更改您的方法以返回具有两个InputStream字段的对象。

于 2013-10-11T13:04:20.320 回答