oracle中存取图片和读取图片 问题:我是学java的,数据库用的是oracle.平时都是直接在oracle中存取的图片的地址。请问怎么样在oracle中存取图片和读取图片?特此请教各位大虾,希望多给出几种解决方案(不要直接在页面上用 JSP 写的那一种) 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 一般都是将 图片的路径存到 db中,存取图片可以用 File 类型进行上传,保存路径。然后读取的时候直接<img src='路径'/>就可以了 第1步,先把文件上传到服务器第2步,把上传的文件读进流里,写到数据库的blob字段里参考以下代码public int uploadXXXXX(int id, InputStream inputStream) throws ArchivesException { PreparedStatement pstmt = null; Connection conn = null; String strSQL = null; ResultSet rs = null; try { conn = this.getConnection(); strSQL = "SELECT CONTENT FROM TableName WHERE ID= ? FOR UPDATE"; pstmt = conn.prepareStatement(strSQL); pstmt.setInt(1, id); rs = pstmt.executeQuery(); if (rs.next()) { OracleThinBlob blobContent = (OracleThinBlob) rs.getBlob("CONTENT"); OutputStream outStream = blobContent.getBinaryOutputStream(); byte[] b = new byte[blobContent.getBufferSize()]; int len = 0; while ((len = inputStream.read(b)) != -1) { outStream.write(b, 0, len); } outStream.flush(); outStream.close(); } } catch (SQLException ex) { logger.error("更新文档内容时出错", ex); throw new ArchivesException("更新文档内容时出错", ex); } catch (Exception ex) { logger.error("更新文档内容时出错", ex); throw new ArchivesException("更新文档内容时出错", ex); } finally { this.closeConnection(rs, pstmt, conn); } return 1; }"CONTENT"就是数库所存放图片的字段名,必须是blob类型 类型用blob 在读取的时候使用JDBC建立一条通道,然后从数据库中读取数据。存也是一个道理 sql语句优化:求助高手 session销毁的问题 tomcat中文参数乱码问题 三岔路口--悲惨职场---迷茫-彷徨 struts hibernate spring 集成问题 !applicationContext-*.xml 不能正常创建加载bean linux下java webservice 乱码问题 初学者请大侠帮助 我通过继承HibernateDaoSupport使用HIBERNATE的查询返回一个ResultSet 寻些建议,求点忠告! 简直是太怪了,ejb调用javabean出错? ext的一个小问题 hibernate hbm.xml配置
然后读取的时候直接<img src='路径'/>就可以了
第2步,把上传的文件读进流里,写到数据库的blob字段里参考以下代码public int uploadXXXXX(int id, InputStream inputStream) throws ArchivesException {
PreparedStatement pstmt = null;
Connection conn = null;
String strSQL = null;
ResultSet rs = null; try {
conn = this.getConnection();
strSQL = "SELECT CONTENT FROM TableName WHERE ID= ? FOR UPDATE"; pstmt = conn.prepareStatement(strSQL);
pstmt.setInt(1, id); rs = pstmt.executeQuery();
if (rs.next()) { OracleThinBlob blobContent = (OracleThinBlob) rs.getBlob("CONTENT");
OutputStream outStream = blobContent.getBinaryOutputStream(); byte[] b = new byte[blobContent.getBufferSize()]; int len = 0; while ((len = inputStream.read(b)) != -1) {
outStream.write(b, 0, len);
} outStream.flush();
outStream.close();
}
} catch (SQLException ex) {
logger.error("更新文档内容时出错", ex);
throw new ArchivesException("更新文档内容时出错", ex);
} catch (Exception ex) {
logger.error("更新文档内容时出错", ex);
throw new ArchivesException("更新文档内容时出错", ex);
} finally {
this.closeConnection(rs, pstmt, conn);
}
return 1;
}
"CONTENT"就是数库所存放图片的字段名,必须是blob类型