从数据库中提取多媒体信息(java,oracle)

用到的JAR包:ojdbc14.jar , dom4j.jar

 

1.把oracle数据库中的Blob字段内容转为byte[]

//提取多媒体信息
    /**
     * conn - 数据库连接
     * strSql - 用于查询多媒体信息的Sql语句
              exp. "select multimedia from testblob where sid='test.txt'"
     */
    private byte[] getMetaForOracle(Connection conn, String strSql)
        throws OPException
    {
        Statement stmt = null;
        ResultSetMetaData rset_meta = null;
        int length = 0;
        try {
            blob = BLOB.empty_lob();
            conn.setAutoCommit(false);
            stmt = conn.createStatement();
            rset = (ResultSet) stmt.executeQuery(strSql);
            rset_meta = (ResultSetMetaData) rset.getMetaData(); // ResultSetMetaData is ojdbc14.jar
            if (rset.next()) {
                Blob objBlob = rset.getBlob(rset_meta.getColumnName(1));
                length = (int) objBlob.length();
                byte[] buffer = objBlob.getBytes(1, length);
                stmt.close();

                return buffer;
            }
            return null;
        }
        catch (Exception e) {
            throw new OPException(e.getMessage());
        }finally{
            try {
                if (stmt != null) {
                    stmt.close();
                }
            }
            catch (SQLException ex) {
            }

        }
    }

 

2.把byte[]转为dom对象或XML字符串.代码片段

sql = " select b.multimedia from pub_blob b where id='1'" ;
byte[] xmlBlob = getMetaForOracle(conn,sql);
SAXReader reader = new SAXReader(); // dom4j.jar
ByteArrayInputStream in = new ByteArrayInputStream(xmlBlob);
InputStreamReader isr = new InputStreamReader(in,"UTF-8");
Document doc = reader.read(isr);//DocumentHelper.parseText(str);
returnXML = doc.asXML(); // dom4j.jar

 

 

 

 

 

 

你可能感兴趣的:(java,xml,meta,oarcle)