我用servlet读取数据库图片文件已经成功

我用servlet读取数据库图片文件已经成功
可以单独显示
现在要在jsp页面上显示出来,同时显示其他详细信息
需要如何设置
使用 <img>还是其他的

servlet主要代码

Java code
 
  
<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --> import java.io. * ; import java.util. * ; import javax.servlet. * ; import javax.servlet.http. * ; import java.sql. * ; public class imageShow extends HttpServlet { private ServletConfig config; // 初始化Servlet final public void init(ServletConfig config) throws ServletException { this .config = config; } // 处理GET请求 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); } // 响应POST请求 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType( " text/html;charset=GBK " ); ServletOutputStream sout = response.getOutputStream(); try { Class.forName( " com.mysql.jdbc.Driver " ); } catch (ClassNotFoundException ce) { sout.println(ce.getMessage()); } try { // 建立数据库连接 String url = " jdbc:mysql://localhost:3306/test " ; String user = " root " ; String pass = "" ; Connection conn = DriverManager.getConnection(url,user,pass); String temp = request.getParameter( " id " ); int id = Integer.parseInt(temp); String sql = " select picture from book where id = " + id; Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(sql); InputStream in = null ; if (rs.next()) { in = rs.getBinaryStream( " picture " ); // ServletOutputStream sout=response.getOutputStream(); byte image[] = new byte [ 1024 ]; while (in.read(image) !=- 1 ) { sout.write(image); } sout.flush(); // sout.close(); rs.close(); stmt.close(); conn.close(); // response.sendRedirect("pleaselogin.jsp?errorMessage=1"); return; // request.getRequestDispatcher("/site2/index.jsp").forward(request,response); } } catch (Exception e){ sout.println( " 图片不能显示.<br> " ); sout.println( " Error : " + e.toString()); } } }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值