存储在Blob字段中的图片怎么用jsp显示!!分不够再加!!

jilongge 2003-05-08 06:32:48
1.图片存入数据库的程序:
File file = new File("c:\test.JPG") ;
FileInputStream fis = new FileInputStream(file);
String insertSQL = "Insert INTO doctorinfo(photoid,photo) VALUES(?,?)";
PreparedStatement pstmt = conn.prepareStatement(insertSQL) ;
pstmt.setString(1, "5");
pstmt.setBinaryStream(2, fis, (int)file.length());
if(1 != pstmt.executeUpdate())
System.err.println("Incorrect value returned during author insert.")

2.从数据库取出图片:
String query = "select photo from doctorinfo where photoid= 5";
ResultSet result = st.executeQuery(query);
result.next();
Blob blob = (Blob)result.getBlob("photo");

下来怎么办???怎么将用<image src="">显示图片???

谢谢!!!!!

...全文
115 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
shadou 2003-05-09
  • 打赏
  • 举报
回复
good
leshui 2003-05-09
  • 打赏
  • 举报
回复
//取得数据库中的数据
Sql="select * from marklist order by markdate desc";
result=DbaObj.ExecuteQuery(Sql);
result.next();
//将数据库中的数据读到流中
InputStream in =result.getBinaryStream("markbody");
//设置输出的格式
response.reset();
response.setContentType("image/jpeg");
//循环去出流中的数据
byte[] b = new byte[1024];
int len;
while((len=in.read(b)) >0)
response.getOutputStream().write(b,0,len);
in.close();
zhaoxichao 2003-05-08
  • 打赏
  • 举报
回复
同意楼上
justtest 2003-05-08
  • 打赏
  • 举报
回复
调用图片
<img src="../servlet/ImageShow?id=<%=rs.getObject("SEX_ID")%>&type=A" width="80" height="100" border="0">
justtest 2003-05-08
  • 打赏
  • 举报
回复
/*
* ====================================================================
*Copyright 2003 iZz Studio, All rights reserved. Design:Ali
*
*文件名:ImageShow.java
*实现功能:读取数据库中存放的二进制图片
*
*最后修改日期:2003-4-6
*/

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.sql.*;
import com.ali.sql.*;

public class ImageShow extends HttpServlet {

private ServletConfig config;

public void init(ServletConfig config) throws ServletException {
this.config = config;
super.init(config);
}

//////////////////////////////doGet/////////////////////////
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置图片类型头
response.setContentType("image/pjpeg");
SQLForm sqlBean = new SQLForm();
sqlBean.setDatatable("SEXMOVIE");
String sql = "SELECT * FROM "+sqlBean.getDatatable()+" WHERE SEX_ID='"+request.getParameter("id")+"'";
try
{
ResultSet rs = sqlBean.Query(sql);
if (rs.next()){
//获取数据库中的图片流,type有A和B两值,分别对应两个字段FILE_A,和FILE_B
InputStream in = rs.getBinaryStream("FILE_"+request.getParameter("type"));
ServletOutputStream out = response.getOutputStream();//输出流
int len;
byte[] buf=new byte[1024];
//开始输出图片
while ((len=in.read(buf,0,1024))!=-1) {
out.write(buf,0,len);
}
out.flush();
out.close();
in.close();
}
rs.close();
}
catch (SQLException e) {
//DO Some code
}
sqlBean.Close();
}
}

67,516

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧