java提取数据库中blob类型的图片,如何全部显示在jsp页面? 5
现在取出来的图片只能显示一张publicvoidgetImages()throwsException{List<TbFiles>list=hszcServices.get...
现在取出来的图片只能显示一张
public void getImages() throws Exception{
List<TbFiles> list= hszcServices.getImages();
Blob fiBlob=null;
String filename=null;
response.setContentType("image/*");
OutputStream toClient = response.getOutputStream();
for(TbFiles tr :list){
fiBlob=tr.getFiles();
filename=tr.getFilename();
InputStream is=fiBlob.getBinaryStream();
byte[] buff = new byte[1024];
int i=0;
while((i = is.read(buff)) != -1){
toClient.write(buff);
}
toClient.close();
is.close();
} }
前台使用<div><img src="<%=basePath%>huishang/getimage.action" alt="The Fatrix" width="700" height="525" /></div> 展开
public void getImages() throws Exception{
List<TbFiles> list= hszcServices.getImages();
Blob fiBlob=null;
String filename=null;
response.setContentType("image/*");
OutputStream toClient = response.getOutputStream();
for(TbFiles tr :list){
fiBlob=tr.getFiles();
filename=tr.getFilename();
InputStream is=fiBlob.getBinaryStream();
byte[] buff = new byte[1024];
int i=0;
while((i = is.read(buff)) != -1){
toClient.write(buff);
}
toClient.close();
is.close();
} }
前台使用<div><img src="<%=basePath%>huishang/getimage.action" alt="The Fatrix" width="700" height="525" /></div> 展开
2个回答
展开全部
在OracleQueryBean类中增加一个函数,来进行读取,具体代码如下:
/**
* 根据图片在数据库中的ID进行读取
* @param strID 图片字段ID
* @param w 需要缩到的宽度
* @param h 需要缩到高度
* @return
*/
public byte[] GetImgByteById(String strID, int w, int h){
//System.out.println("Get img data which id is " + nID);
if(myConnection == null)
this.getConnection();
byte[] data = null;
try {
Statement stmt = myConnection.createStatement();
ResultSet myResultSet = stmt.executeQuery("select " + this.strIDName + " from " + this.strTabName + " where " + this.strIDName + "=" + strID);
StringBuffer myStringBuffer = new StringBuffer();
if (myResultSet.next()) {
java.sql.Blob blob = myResultSet.getBlob(this.strImgName);
InputStream inStream = blob.getBinaryStream();
try {
long nLen = blob.length();
int nSize = (int) nLen;
//System.out.println("img data size is :" + nSize);
data = new byte[nSize];
inStream.read(data);
inStream.close();
} catch (IOException e) {
System.out.println("获取图片数据失败,原因:" + e.getMessage());
}
data = ChangeImgSize(data, w, h);
}
System.out.println(myStringBuffer.toString());
myConnection.commit();
myConnection.close();
} catch (SQLException ex) {
System.out.println(ex.getMessage());
}
return data;
}
页面使用OracleQueryBean来根据用户提供的图片id进行查询,在读取并进行缩放后,通过jsp页面进行展示,具体代码如下:
<%@ page language="java" contentType="text/html;;charset=gbk" %>
<jsp:useBean id="OrcleQuery" scope="page" class="HLFtiDemo.OracleQueryBean" />
<%
response.setContentType("image/jpeg");
//图片在数据库中的 ID
String strID = request.getParameter("id");
//要缩略或放大图片的宽度
String strWidth = request.getParameter("w");
//要缩略或放大图片的高度
String strHeight = request.getParameter("h");
byte[] data = null;
if(strID != null){
int nWith = Integer.parseInt(strWidth);
int nHeight = Integer.parseInt(strHeight);
//获取图片的byte数据
data = OrcleQuery.GetImgByteById(strID, nWith, nHeight);
ServletOutputStream op = response.getOutputStream();
op.write(data, 0, data.length);
op.close();
op = null;
response.flushBuffer();
//清除输出流,防止释放时被捕获异常
out.clear();
out = pageContext.pushBody();
}
%>
/**
* 根据图片在数据库中的ID进行读取
* @param strID 图片字段ID
* @param w 需要缩到的宽度
* @param h 需要缩到高度
* @return
*/
public byte[] GetImgByteById(String strID, int w, int h){
//System.out.println("Get img data which id is " + nID);
if(myConnection == null)
this.getConnection();
byte[] data = null;
try {
Statement stmt = myConnection.createStatement();
ResultSet myResultSet = stmt.executeQuery("select " + this.strIDName + " from " + this.strTabName + " where " + this.strIDName + "=" + strID);
StringBuffer myStringBuffer = new StringBuffer();
if (myResultSet.next()) {
java.sql.Blob blob = myResultSet.getBlob(this.strImgName);
InputStream inStream = blob.getBinaryStream();
try {
long nLen = blob.length();
int nSize = (int) nLen;
//System.out.println("img data size is :" + nSize);
data = new byte[nSize];
inStream.read(data);
inStream.close();
} catch (IOException e) {
System.out.println("获取图片数据失败,原因:" + e.getMessage());
}
data = ChangeImgSize(data, w, h);
}
System.out.println(myStringBuffer.toString());
myConnection.commit();
myConnection.close();
} catch (SQLException ex) {
System.out.println(ex.getMessage());
}
return data;
}
页面使用OracleQueryBean来根据用户提供的图片id进行查询,在读取并进行缩放后,通过jsp页面进行展示,具体代码如下:
<%@ page language="java" contentType="text/html;;charset=gbk" %>
<jsp:useBean id="OrcleQuery" scope="page" class="HLFtiDemo.OracleQueryBean" />
<%
response.setContentType("image/jpeg");
//图片在数据库中的 ID
String strID = request.getParameter("id");
//要缩略或放大图片的宽度
String strWidth = request.getParameter("w");
//要缩略或放大图片的高度
String strHeight = request.getParameter("h");
byte[] data = null;
if(strID != null){
int nWith = Integer.parseInt(strWidth);
int nHeight = Integer.parseInt(strHeight);
//获取图片的byte数据
data = OrcleQuery.GetImgByteById(strID, nWith, nHeight);
ServletOutputStream op = response.getOutputStream();
op.write(data, 0, data.length);
op.close();
op = null;
response.flushBuffer();
//清除输出流,防止释放时被捕获异常
out.clear();
out = pageContext.pushBody();
}
%>
展开全部
在下载图片之前,查询数据库中所有图片的主键。在前台遍历返回结果并用js动态生成img标签。
追问
能详细点吗?我这是从后台直接去图片在页面显示,图片是blob存储
追答
额,我的思路是这样。完成整个功能需要发送两次请求。第一次请求查询出每一张图片在数据库的主键或者是url;第二步,前台接到数据以后,遍历结果集,为每一行数据生成一个img标签。for(var item in result)document.createElement('img')...,然后给标签src标签赋值(给src赋值之后,浏览器会像服务器发出请求,以下载图片,这就是第二次请求)。
这样回答可以吗?够细致么?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询