public static void readDB2Image() {
String targetPath = "D:/123/1png";
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
conn = DBUtilgetConn();
String sql = "select from map where id =";
ps = connprepareStatement(sql);
pssetInt(1, 1);
rs = psexecuteQuery();
while (rsnext()) {
InputStream in = rsgetBinaryStream("photo");
ImageUtilreadBin2Image(in, targetPath);
}
} catch (Exception e) {
eprintStackTrace();
} finally {
DBUtilcloseConn(conn);
if (rs != null) {
try {
rsclose();
} catch (SQLException e) {
eprintStackTrace();
}
}
if (ps != null) {
try {
psclose();
} catch (SQLException e) {
eprintStackTrace();
}
}
}
在OracleQueryBean类中增加一个函数,来进行读取,具体代码如下:
/
根据在数据库中的ID进行读取
@param strID 字段ID
@param w 需要缩到的宽度
@param h 需要缩到高度
@return
/
public byte[] GetImgByteById(String strID, int w, int h){
//Systemoutprintln("Get img data which id is " + nID);
if(myConnection == null)
thisgetConnection();
byte[] data = null;
try {
Statement stmt = myConnectioncreateStatement();
ResultSet myResultSet = stmtexecuteQuery("select " + thisstrIDName + " from " + thisstrTabName + " where " + thisstrIDName + "=" + strID);
StringBuffer myStringBuffer = new StringBuffer();
if (myResultSetnext()) {
javasqlBlob blob = myResultSetgetBlob(thisstrImgName);
InputStream inStream = blobgetBinaryStream();
try {
long nLen = bloblength();
int nSize = (int) nLen;
//Systemoutprintln("img data size is :" + nSize);
data = new byte[nSize];
inStreamread(data);
inStreamclose();
} catch (IOException e) {
Systemoutprintln("获取数据失败,原因:" + egetMessage());
}
data = ChangeImgSize(data, w, h);
}
Systemoutprintln(myStringBuffertoString());
myConnectioncommit();
myConnectionclose();
} catch (SQLException ex) {
Systemoutprintln(exgetMessage());
}
return data;
}
页面使用OracleQueryBean来根据用户提供的id进行查询,在读取并进行缩放后,通过jsp页面进行展示,具体代码如下:
<%@ page language="java" contentType="text/html;;charset=gbk" %>
<jsp:useBean id="OrcleQuery" scope="page" class="HLFtiDemoOracleQueryBean" />
<%
responsesetContentType("image/jpeg");
//在数据库中的 ID
String strID = requestgetParameter("id");
//要缩略或放大的宽度
String strWidth = requestgetParameter("w");
//要缩略或放大的高度
String strHeight = requestgetParameter("h");
byte[] data = null;
if(strID != null){
int nWith = IntegerparseInt(strWidth);
int nHeight = IntegerparseInt(strHeight);
//获取的byte数据
data = OrcleQueryGetImgByteById(strID, nWith, nHeight);
ServletOutputStream op = responsegetOutputStream();
opwrite(data, 0, datalength);
opclose();
op = null;
responseflushBuffer();
//清除输出流,防止释放时被捕获异常
outclear();
out = pageContextpushBody();
}
%>
以上就是关于java 数据库读取图片出来全部是1KB全部的内容,包括:java 数据库读取图片出来全部是1KB、java提取数据库中blob类型的图片,如何全部显示在jsp页面、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)