Streaming Data through Spring JDBC, unknown length

前端 未结 1 1303
不知归路
不知归路 2021-02-10 06:00

I currently have an application that inserts byte[] into our DB through the use of Spring JDBC [SqlLobValue]. The problem is, this is not a scalable way to take in data, as the

相关标签:
1条回答
  • 2021-02-10 06:55

    I presume you meant "InputStream" rather than "OutputStream". I tried this out, but I was having bigger problems with my JDBC driver, so I am unsure if this actually works.

    InputStream inputStream = httpServletRequest.getInputStream();
    
    int contentLength = -1; // fake, will be ignored anyway
    SqlLobValue sqlLobValue = new SqlLobValue(
        inputStream,
        contentLength,
        new DefaultLobHandler() {
            public LobCreator getLobCreator() {
                return new DefaultLobHandler.DefaultLobCreator() {
                    public void setBlobAsBinaryStream(PreparedStatement ps, int paramIndex, InputStream binaryStream, int contentLength) throws SQLException {
                        // The contentLength parameter should be the -1 we provided earlier.
                        // You now have direct access to the PreparedStatement.
                        // Simply avoid calling setBinaryStream(int, InputStream, int)
                        // in favor of setBinaryStream(int, InputStream).
                        ps.setBinaryStream(paramIndex, binaryStream);
                    }
                };
            }
        }
    );
    
    jdbcTemplate.update(
        "INSERT INTO foo (bar) VALUES (?)",
        new Object[]{ sqlLobValue }
    );
    
    0 讨论(0)
提交回复
热议问题