如何在jsp中把照片插入Oracle
在jsp中,如何执行这样的操作,就是把照片作为数据存入oracle中,oracle我用的是10g。
谁能给示范代码,谢了!
问题点数:50、回复次数:3Top
1 楼pingpingx(柠檬)回复于 2006-03-03 10:06:19 得分 0
你可以使用数据库BLOB 类型存储图片,在把它读取出来。java.sql.Blob 因为这个类没有被序列化(Serializable),要使用数据库的BLOB字段,我们必须在实体bean中声明CMP字段为 byte[] 并映射该字段到数据库的BLOB字段。
有一些处理的细节和注意点,下面的网页对你有些帮助。
http://www.crackj2ee.com/Article/ShowArticle.asp?ArticleID=262Top
2 楼championmajian(小马||目前酒力:白的半斤,啤的3瓶)回复于 2006-03-03 10:25:57 得分 0
存目录地址多好Top
3 楼reack(三楼楼长)回复于 2006-03-03 10:29:49 得分 0
/**
* 向数据库中插入一个新的BLOB对象
*
* @param infile - 数据文件
* @throws java.lang.Exception
* @roseuid 3EDA04E300F6
*/
public static void blobInsert(String infile, String photoid) throws Exception {
DBConnection dbconnection = new DBConnection();
myconn = dbconnection.createConnection();
// Class.forName(DRIVER);
Statement mystmt = null;
ResultSet rs = null;
/* 设定不自动提交 */
boolean defaultCommit = myconn.getAutoCommit();
myconn.setAutoCommit(false);
try {
/* 插入一个空的BLOB对象 */
String sql = "";
sql = "INSERT INTO ccic_real_photo VALUES ('" + photoid +
"', EMPTY_BLOB(),'" + photoid.substring(0,21) +"')";
mystmt = myconn.prepareStatement(sql);
mystmt.executeUpdate(sql);
mystmt.close();
myconn.commit();
//mystmt = null;
/* 查询此BLOB对象并锁定 */
sql = "SELECT blob_column FROM ccic_real_photo WHERE photoid='" + photoid +
"' FOR UPDATE";
mystmt = myconn.prepareStatement(sql);
rs = mystmt.executeQuery(sql);
while (rs.next()) {
// 取出此BLOB对象
oracle.sql.BLOB blob = (oracle.sql.BLOB) rs.getBlob("blob_column");
// 向BLOB对象中写入数据
BufferedOutputStream out = new BufferedOutputStream(blob.
getBinaryOutputStream());
BufferedInputStream in = new BufferedInputStream(new FileInputStream(
infile));
int c;
while ( (c = in.read()) != -1) {
out.write(c);
}
in.close();
out.close();
}
// 正式提交
myconn.commit();
mystmt.close();
rs.close();
mystmt = null;
rs = null;
//myconn.close();
}
catch (Exception ex) {
/* 出错回滚 */
mystmt.close();
//rs.close();
mystmt = null;
rs = null;
myconn.rollback();
//myconn.close();
myconn = null;
dbconnection = null;
try {
util.LogError("出错" + photoid + ";照片文件" + infile + "错误原因:" + ex.toString());
}
catch (Exception e) {
System.out.println(e.toString());
}
System.out.println("出错" + photoid + ";照片文件" + infile + "错误原因:" +
ex.toString());
//throw ex;
//myconn = null;
}
/* 恢复原提交状态 */
//conn.setAutoCommit(defaultCommit);
myconn = null;
dbconnection = null;
}
Top




