用JSP从数据库中读取图片并显示在网页上
<1>先在mysql下建立如下的table. 并insert图像.
mysql.sql文件如下:
- CREATE TABLE photo (
- photo_no int(6) unsigned NOT NULL auto_increment,
- image blob,
- PRIMARY KEY (`photo_no`)
- )
<2>把show.jsp放在tomcat的任意目录下. show.jsp作用:从数据库中读出blob,并产生image/jpg.
show.jsp文件如下:
- <%@ page contentType="text/html; charset=gbk" %>
- <%@ page import="java.io.*"%>
- <%@ page import="java.sql.*, javax.sql.*" %>
- <%@ page import="java.util.*"%>
- <%@ page import="java.math.*"%>
- <%
- String photo_no = request.getParameter("photo_no");
- //mysql连接
- Class.forName("com.mysql.jdbc.Driver").newInstance();
- String URL="jdbc:mysql://localhost:3306/job?user=root&password=111111";
- Connection con = DriverManager.getConnection(URL);
- //oracle连接
- //String URL="jdbc:oracle:thin@localhost:1521:orcl2";
- //user="system";
- //password="manager";
- //Connection con = DriverManager.getConnection(URL,user,password);
- try{
- // 准备语句执行对象
- Statement stmt = con.createStatement();
- String sql = " SELECT * FROM PHOTO WHERE photo_no = "+ photo_no;
- ResultSet rs = stmt.executeQuery(sql);
- if (rs.next()) {
- Blob b = rs.getBlob("photo_image");
- long size = b.length();
- //out.print(size);
- byte[] bs = b.getBytes(1, (int)size);
- response.setContentType("image/jpeg");
- OutputStream outs = response.getOutputStream();
- outs.write(bs);
- outs.flush();
- rs.close();
- }
- else {
- rs.close();
- response.sendRedirect("./images/error.gif");
- }
- }
- finally{
- con.close();
- }
- %>
<3>把如下文件放在show.jsp的同一目录下.
index.html文件如下:
- <html>
- <head>
- <title> 图像测试 </title>
- </head>
- <body>
- <table>
- <tr>
- <td>
- 图像测试
- </td>
- </tr>
- <tr>
- <td>
- <img src="show.jsp?photo_no=2">
- </td>
- </tr>
- </table>
- </body>
- </html>
用JSP从数据库中读取图片并显示在网页上的更多相关文章
- [转]asp.net mvc 从数据库中读取图片
本文转自:http://www.cnblogs.com/mayt/archive/2010/05/20/1740358.html 首先是创建一个类,继承于ActionResult,记住要引用Syste ...
- asp.net mvc 从数据库中读取图片的实现代码
首先是创建一个类,继承于ActionResult,记住要引用System.Web.Mvc命名空间,如下: public class ImageResult : ActionResult { publi ...
- [转] 从数据库中读取图片并导入Excel文件,C#方式
原文地址, 作者 Lvyou1980 直接源码吧. using System; using System.IO; using System.Data; using System.Drawing; us ...
- 从npz文件中读取图片并显示的小例子
前提:我把自己的数据集存成了npz的形式,也就是npy的压缩形式.如果电脑上安装了解压软件,双击npz文件的话,会出现每一部分压缩文件的名字例如npz文件的名称为:mnist.npz文件,用好压解压软 ...
- C# 图片保存到数据库和从数据库读取图片并显示
图片保存到数据库的方法: public void imgToDB(string sql) { //参数sql中要求保存的imge变量名称为@images //调 ...
- MVC中根据后台绝对路径读取图片并显示在IMG中
数据库存取图片并在MVC3中显示在View中 根据路径读取图片: byte[] img = System.IO.File.ReadAllBytes(@"d:\xxxx.jpg"); ...
- C#从SQL server数据库中读取l图片和存入图片
原文:C#从SQL server数据库中读取l图片和存入图片 本实例主要介绍如何将图片存入数据库.将图片存入数据库,首先要在数据库中建立一张表,将存储图片的字段类型设为Image类型,用FileStr ...
- Servlet从本地文件中读取图片,并显示在页面中
import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpSer ...
- [Xcode 实际操作]九、实用进阶-(12)从系统相册中读取图片
目录:[Swift]Xcode实际操作 本文将演示从系统相册中读取图片. 在项目导航区,打开视图控制器的代码文件[ViewController.swift] import UIKit //添加两个协议 ...
随机推荐
- python网络编程——实现简单聊天
通过socket建立简单的聊天工具 server.py import socket import threading import time s = socket.socket(socket.AF_I ...
- spring boot热部署devtools
1 pom.xml文件 注:热部署功能spring-boot-1.3开始有的 <!--添加依赖--> <dependency> <groupId>org.sprin ...
- 基于Bert的文本情感分类
详细代码已上传到github: click me Abstract: Sentiment classification is the process of analyzing and reaso ...
- ABP开发框架前后端开发系列---(5)Web API调用类在Winform项目中的使用
在前面几篇随笔介绍了我对ABP框架的改造,包括对ABP总体的介绍,以及对各个业务分层的简化,Web API 客户端封装层的设计,使得我们基于ABP框架的整体方案越来越清晰化, 也越来越接近实际的项目开 ...
- kubernetes实战篇之为默认账户创建镜像拉取密钥
系列目录 上一节我们分别使用纯文本账户密码和docker的config文件一创建一个kubernetes secret对象,并且把它添加到containers的imagePullSecrets字段用以 ...
- Django 你需要掌握的模型层(标签、过滤器、模板的继承与导入)
Django 模型层(标签.过滤器.模板的继承与导入) 好文章来自超哥:https://www.cnblogs.com/guanchao/p/11006062.html 过滤器/自定义过滤器 模板 ...
- 【入门】WebRTC知识点概览 | 内有技术干货免费下载
什么是WebRTC WebRTC 即Web Real-Time Communication(网页实时通信)的缩写,是一个支持网页浏览器之间进行实时数据传输(包括音频.视频.数据流)的技术.经过多年的发 ...
- JVM 参数类型
标准参数 -help -server -client -version -showversion -cp -classpath X参数 非标准化参数(在各个JDK版本中可能会变,但是变动比较小) -X ...
- Zookeeper_ACL
getAcl path 查看某个node的权限 设置权限1 world方式 setAcl <path> world:anyone:<acl>例如 setAcl /node1 w ...
- String.format()
System.out.println(String.format("sftp DownloadDir is: %s and new is %s", "哈哈",& ...