<1>先在mysql下建立如下的table. 并insert图像. 
mysql.sql文件如下:

  1. CREATE TABLE photo (
  2. photo_no int(6) unsigned NOT NULL auto_increment,
  3. image blob,
  4. PRIMARY KEY (`photo_no`)
  5. )

<2>把show.jsp放在tomcat的任意目录下. show.jsp作用:从数据库中读出blob,并产生image/jpg.

show.jsp文件如下:

  1. <%@ page contentType="text/html; charset=gbk" %>
  2. <%@ page import="java.io.*"%>
  3. <%@ page import="java.sql.*, javax.sql.*" %>
  4. <%@ page import="java.util.*"%>
  5. <%@ page import="java.math.*"%>
  6. <%
  7. String photo_no = request.getParameter("photo_no");
  8. //mysql连接
  9. Class.forName("com.mysql.jdbc.Driver").newInstance();
  10. String URL="jdbc:mysql://localhost:3306/job?user=root&password=111111";
  11. Connection con = DriverManager.getConnection(URL);
  12. //oracle连接
  13. //String URL="jdbc:oracle:thin@localhost:1521:orcl2";
  14. //user="system";
  15. //password="manager";
  16. //Connection con = DriverManager.getConnection(URL,user,password);
  17. try{
  18. // 准备语句执行对象
  19. Statement stmt = con.createStatement();
  20. String sql = " SELECT * FROM PHOTO WHERE photo_no = "+ photo_no;
  21. ResultSet rs = stmt.executeQuery(sql);
  22. if (rs.next()) {
  23. Blob b = rs.getBlob("photo_image");
  24. long size = b.length();
  25. //out.print(size);
  26. byte[] bs = b.getBytes(1, (int)size);
  27. response.setContentType("image/jpeg");
  28. OutputStream outs = response.getOutputStream();
  29. outs.write(bs);
  30. outs.flush();
  31. rs.close();
  32. }
  33. else {
  34. rs.close();
  35. response.sendRedirect("./images/error.gif");
  36. }
  37. }
  38. finally{
  39. con.close();
  40. }
  41. %>

<3>把如下文件放在show.jsp的同一目录下. 
index.html文件如下:

    1. <html>
    2. <head>
    3. <title> 图像测试 </title>
    4. </head>
    5. <body>
    6. <table>
    7. <tr>
    8. <td>
    9. 图像测试
    10. </td>
    11. </tr>
    12. <tr>
    13. <td>
    14. <img src="show.jsp?photo_no=2">
    15. </td>
    16. </tr>
    17. </table>
    18. </body>
    19. </html>

用JSP从数据库中读取图片并显示在网页上的更多相关文章

  1. [转]asp.net mvc 从数据库中读取图片

    本文转自:http://www.cnblogs.com/mayt/archive/2010/05/20/1740358.html 首先是创建一个类,继承于ActionResult,记住要引用Syste ...

  2. asp.net mvc 从数据库中读取图片的实现代码

    首先是创建一个类,继承于ActionResult,记住要引用System.Web.Mvc命名空间,如下: public class ImageResult : ActionResult { publi ...

  3. [转] 从数据库中读取图片并导入Excel文件,C#方式

    原文地址, 作者 Lvyou1980 直接源码吧. using System; using System.IO; using System.Data; using System.Drawing; us ...

  4. 从npz文件中读取图片并显示的小例子

    前提:我把自己的数据集存成了npz的形式,也就是npy的压缩形式.如果电脑上安装了解压软件,双击npz文件的话,会出现每一部分压缩文件的名字例如npz文件的名称为:mnist.npz文件,用好压解压软 ...

  5. C# 图片保存到数据库和从数据库读取图片并显示

    图片保存到数据库的方法: public void imgToDB(string sql)        {   //参数sql中要求保存的imge变量名称为@images            //调 ...

  6. MVC中根据后台绝对路径读取图片并显示在IMG中

    数据库存取图片并在MVC3中显示在View中 根据路径读取图片: byte[] img = System.IO.File.ReadAllBytes(@"d:\xxxx.jpg"); ...

  7. C#从SQL server数据库中读取l图片和存入图片

    原文:C#从SQL server数据库中读取l图片和存入图片 本实例主要介绍如何将图片存入数据库.将图片存入数据库,首先要在数据库中建立一张表,将存储图片的字段类型设为Image类型,用FileStr ...

  8. Servlet从本地文件中读取图片,并显示在页面中

    import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpSer ...

  9. [Xcode 实际操作]九、实用进阶-(12)从系统相册中读取图片

    目录:[Swift]Xcode实际操作 本文将演示从系统相册中读取图片. 在项目导航区,打开视图控制器的代码文件[ViewController.swift] import UIKit //添加两个协议 ...

随机推荐

  1. python网络编程——实现简单聊天

    通过socket建立简单的聊天工具 server.py import socket import threading import time s = socket.socket(socket.AF_I ...

  2. spring boot热部署devtools

    1 pom.xml文件 注:热部署功能spring-boot-1.3开始有的 <!--添加依赖--> <dependency> <groupId>org.sprin ...

  3. 基于Bert的文本情感分类

    详细代码已上传到github: click me Abstract:    Sentiment classification is the process of analyzing and reaso ...

  4. ABP开发框架前后端开发系列---(5)Web API调用类在Winform项目中的使用

    在前面几篇随笔介绍了我对ABP框架的改造,包括对ABP总体的介绍,以及对各个业务分层的简化,Web API 客户端封装层的设计,使得我们基于ABP框架的整体方案越来越清晰化, 也越来越接近实际的项目开 ...

  5. kubernetes实战篇之为默认账户创建镜像拉取密钥

    系列目录 上一节我们分别使用纯文本账户密码和docker的config文件一创建一个kubernetes secret对象,并且把它添加到containers的imagePullSecrets字段用以 ...

  6. Django 你需要掌握的模型层(标签、过滤器、模板的继承与导入)

    Django 模型层(标签.过滤器.模板的继承与导入) 好文章来自超哥:https://www.cnblogs.com/guanchao/p/11006062.html   过滤器/自定义过滤器 模板 ...

  7. 【入门】WebRTC知识点概览 | 内有技术干货免费下载

    什么是WebRTC WebRTC 即Web Real-Time Communication(网页实时通信)的缩写,是一个支持网页浏览器之间进行实时数据传输(包括音频.视频.数据流)的技术.经过多年的发 ...

  8. JVM 参数类型

    标准参数 -help -server -client -version -showversion -cp -classpath X参数 非标准化参数(在各个JDK版本中可能会变,但是变动比较小) -X ...

  9. Zookeeper_ACL

    getAcl path 查看某个node的权限 设置权限1 world方式 setAcl <path> world:anyone:<acl>例如 setAcl /node1 w ...

  10. String.format()

    System.out.println(String.format("sftp DownloadDir is: %s and new is %s", "哈哈",& ...