oracle 数据库存放读取二进制文件(将上传的文件流存在数据库)

private void saveBinary(InputStream in, Long userId, String remark) {
        Sql sql = new Sql("insert into 表名 (id,CONTENT,USER_ID,REMARK) values(序列.NEXTVAL,?,?,?)");
        sql.addParameter(new Parameter(DataType.BLOB, in));//需要存的二进制文件流
        sql.addParameter(new Parameter(DataType.LONG, userId));//Long类型
        sql.addParameter(new Parameter(DataType.STRING, remark));//String类型
        dao.execute(sql);
    } 

 public InputStream getBinary(String sql) {
        InputStream fis = null;
        Connection conn = null;
        PreparedStatement pstmt = null;
        try {
            conn = ContextUtil.getConnection();
            // --
            pstmt = conn.prepareStatement(sql);
            ResultSet rs = pstmt.executeQuery();
            if (rs.next()) {
                fis = rs.getBinaryStream(1);
            }
            rs.close();
            // --
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (pstmt != null) {
                try {
                    pstmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return fis;
    }

 

posted @ 2014-09-24 17:10  IT菜菜  阅读(1996)  评论(0编辑  收藏  举报