jsp页面上怎么获得数据库中查询得到的图片文件

handle19811106 2005-03-14 06:23:50
我用的是struts来实现的
但是不知道怎么将oracle 数据库中查询得到的图片输出到页面上来
我的数据库操作是这样的:
package book;

import java.sql.*;
import java.io.*;
import oracle.jdbc.driver.*;
import oracle.sql.*;

public class AccessImgByDB {
public AccessImgByDB() {
}

//插入图片
public static void insertImg() {
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
String url = "jdbc:oracle:thin:@192.168.0.3:1521:EGOV4JS";
Connection con = DriverManager.getConnection(url, "BOOKMAN", "BOOKMAN");

boolean defaultCommitType = con.getAutoCommit();
con.setAutoCommit(false);
Statement stmt = con.createStatement();
stmt.executeUpdate(
"insert into BOOK (BOOKID,BOOKNAME,BOOKIMG) values(119,'数学',empty_blob())");

String cmd = "SELECT * FROM Book WHERE bookname='数学' for update ";
ResultSet rset = stmt.executeQuery(cmd);
System.out.println(cmd);
rset.next();
BLOB blob = null;
try
{
blob = (oracle.sql.BLOB) ((OracleResultSet) rset).
getBlob("BOOKIMG");
}
catch (SQLException ex1)
{
System.out.println("+++++++++++++++发现异常!");
}
File binaryFile = new File(
"D:\\Myproject\\book\\1.jpg");
System.out.println( binaryFile.getPath());
System.out.println( binaryFile.exists());
// SecurityManager manager = new SecurityManager();
// System.out.println("SecurityManager.checkRead(java.lang.String)="+manager.checkRead("D:\\1.JPG"));
FileInputStream instream = new FileInputStream(binaryFile);
OutputStream outstream = blob.getBinaryOutputStream();
int size = blob.getBufferSize();
byte[] buffer = new byte[10000];
int length = -1;
while ( (length = instream.read(buffer)) != -1)
outstream.write(buffer, 0, length);
instream.close();
outstream.close();
con.commit();
con.setAutoCommit(defaultCommitType);
con.close();

}
catch (Exception ex) {
ex.printStackTrace();
}

}

//获取图片
//获取图片
public static void getImg() {

try {
Class.forName("oracle.jdbc.driver.OracleDriver");
String url = "jdbc:oracle:thin:@192.168.0.3:1521:EGOV4JS";

Connection con = DriverManager.getConnection(url, "BOOKMAN", "BOOKMAN");

boolean defaultCommit = con.getAutoCommit();

con.setAutoCommit(false);

/* 查询BLOB对象 */
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(
"SELECT * FROM BOOK WHERE BOOKname='数学 '");

rs.next();

oracle.sql.BLOB blob = (oracle.sql.BLOB) rs.getBlob("BOOKIMG");
BufferedInputStream in = new BufferedInputStream(blob.getBinaryStream());
in.close();
con.commit();
rs.close();
con.close();

}
catch (Exception ex) {
ex.printStackTrace();

/* 正式提交 */

}

}



public static void main(String[] argv) {
System.out.println("insert start");
AccessImgByDB.insertImg();
System.out.println("insert end");
AccessImgByDB.getImg();

}
}
...全文
134 点赞 收藏 1
写回复
1 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
JOBLin 2005-03-14
给你一个例子,我先放到一个指定的目录下面,然后读这个文件。
public String getPhoto(String sID) throws CCustomException
{
try
{
CSqlQuery2 sqQuery = new CSqlQuery2("kkk");

DataSource source = sqQuery.getDataSource();
Connection conn = CDataSourceUtil.GetConnection(source);

Blob blob = null;
Statement select = conn.createStatement();
String sSQL = "select Photo from Photo where ExamNo = '" + sID + "'";
Statement pstmt = conn.createStatement();
ResultSet rs = pstmt.executeQuery(sSQL);
String sFileName ="";
if(rs.next())
{
//读出流用getBinaryStream()方法。
InputStream inStream = rs.getBinaryStream(1);
//图片的有效长度。
int size = inStream.available();
byte[] pictureData = new byte[size];

//read(byte[] b):读取输入流的数据到指定的数组。只是文件的有效部分。
inStream.read(pictureData);

//将图片文件输出到指定目录。
File f = File.createTempFile("kkk",".jpg",new File(System.getProperty("java.io.tmpdir")));
sFileName = f.getAbsolutePath();
FileOutputStream fileOutStream = new FileOutputStream(f.getAbsolutePath());
//写入数据。
fileOutStream.write(pictureData);
fileOutStream.close();
}

rs.close();
pstmt.close();

return sFileName;
}
catch(CCustomException e)
{
throw e;
}
catch(Exception e)
{
throw new CCustomException("","操作文件时出错!",e.getMessage());
}
}
回复
相关推荐
发帖
Java EE
创建于2007-09-28

6.6w+

社区成员

J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
申请成为版主
帖子事件
创建了帖子
2005-03-14 06:23
社区公告
暂无公告