如何将oracle数据库中的blob显示为jsp的图片,请教啦,如何把数据库中blob字段存储的图片在jsp页面中显示出来?...

找了一些相关资料看了一下,初学的理解不一定对,请指教。

大体的思路是把图片以二进制流的方式存到blob字段中。这一步调试成功。我现在不知道如何把已存的图片显示在jsp页面上。现在用一种我认为比较笨的办法可以实现。就是从blob字段中读出这个图片的信息,生成一个新的图片,保存在服务器的相关目录下面,然后用页面的从该目录下面找到该图片显示出来。

但是这样要专门设一个目录存放图片,占用磁盘空间;图片多了以后还有重名的问题;所有图片放在一起也不容易管理不好分类。感觉很麻烦。

我的想法看能不能直接从数据库中读出图片信息然后直接类似于out.println(picture);这样显示在页面上,而不保存到硬盘的实际目录下。

请各位出出主意看如何实现?用什么办法?如果可以请给出范例代码。分数可以再加,欢迎指教,多谢。

|

假如是一个GIF图片

JSP中

//在yourServlet后面可以跟参数

在yourServlet中

从数据库读出二进制流,象out中写

在最前面

response.setContentType("image/gif");

|

package newhua;

import java.io.*;

import java.util.*;

import java.sql.*;

import java.text.*;

import oracle.sql.*;

import oracle.jdbc.driver.*;

/**

*

Title:

*

Description:

*

Copyright: Copyright (c) 2002

*

Company:

* @author unascribed

* @version 1.0

*/

public class writeBean {

private String sample = "";

//Access sample property

public String getSample() {

return sample;

}

//Access sample property

public void setSample(String newValue) {

if (newValue!=null) {

sample = newValue;

}

}

private String filename = "";

//Access sample property

public String getFilename() {

return filename;

}

//Access sample property

public void setFilename(String newValue) {

if (newValue!=null) {

filename = newValue;

}

}

public  void gwrite() throws SQLException,IOException

{

PreparedStatement stmt = null;

ResultSet rs =  null;

InputStream fin = null;

OutputStream fout = null;

Connection conn=null;

try{

newhua.dbbean  db=new newhua.dbbean();

db.InitDatabase();

conn =db.conn;

//conn.setAutoCommit(false);

System.out.println(sample);

System.out.println(filename);

//String filename;

//

//filename="c:/tomcat/webapps/nw/lsdoc/"+sample+".sxw";

String mysql= "select word from tmanuscript where glidenumber='"+sample+"' for update";

System.out.println(mysql);

stmt=conn.prepareStatement(mysql);

//stmt.setString(1,sample);

rs=stmt.executeQuery() ;

//System.out.println();

if(rs.next()) {

System.out.println(sample);

System.out.println(filename);

BLOB blob = ((OracleResultSet)rs).getBLOB("word");

fout = blob.getBinaryOutputStream();

File f = new File(filename);

fin = new FileInputStream(f);

byte[] buffer = new byte[blob.getBufferSize()];

int bytesRead = 0;

while((bytesRead = fin.read(buffer)) != -1)

{

fout.write(buffer, 0, bytesRead);

System.out.println(bytesRead);

}

blob = null;

f = null;

buffer = null;

fin.close();

fout.close();

conn.commit();

}

}

catch(Exception ex)

{

ex.printStackTrace();

}

finally {

try {

fin = null;

fout = null;

rs = null;

conn = null;

stmt = null;

}

catch(Exception e) {

e.printStackTrace();

}

}

}

public void gread() throws SQLException,IOException

{

Connection conn = null;

PreparedStatement stmt = null;

InputStream in = null;

OutputStream out = null;

BLOB blob = null;

ResultSet rs = null;

try {

newhua.dbbean  db=new newhua.dbbean();

db.InitDatabase();

conn =db.conn;

//String filename;

//filename="c:/tomcat/webapps/nw/lsdoc/o_"+sample+".sxw";

String mysql="Select word FROM tmanuscript WHERE glidenumber ='"+sample+"' ";

System.out.println(mysql);

stmt = conn.prepareStatement(mysql);

//stmt.setString(1,"67");

rs = stmt.executeQuery();

if(rs.next()) {

blob = ((OracleResultSet)rs).getBLOB("word");

in = blob.getBinaryStream();

out = new FileOutputStream(filename);

int bufferSize = blob.getBufferSize();

byte[] buffer = new byte[bufferSize];

int bytesRead = 0;

while ((bytesRead = in.read(buffer)) != -1) {

out.write(buffer, 0, bytesRead);

System.out.println(bytesRead);

}

stmt.clearParameters();

buffer = null;

in.close();

out.close();

conn.commit();

}

}

catch(Exception e) {

e.printStackTrace();

}

finally {

try {

in = null;

blob = null;

rs = null;

out = null;

conn = null;

stmt = null;

}

catch(Exception e) {

}

}

}

}

jsp:

发文--保存

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值