转载自:http://heisetoufa.iteye.com/blog/227245
关键字: 显示 浏览器 图片
上两篇文章我发的一个是把图片从硬盘存到数据库里,和把图片从数据库里取出来存在硬盘上,这次再发个把图片从数据库里取出来直接显示在浏览器上,而不保存在硬盘上
先贴个在jsp里用的
- <%@ page language="java" pageEncoding="gbk" contentType="image/jpeg" import="java.awt.image.*,java.sql.*,com.sun.image.codec.jpeg.*,java.util.*,javax.imageio.*,java.io.*"%>
-
-
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
-
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
-
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
-
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
-
-
-
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
-
<html:html lang="true">
-
<head>
-
<html:base />
-
-
<title>EPhotoDis.jsp</title>
-
-
<meta http-equiv="pragma" content="no-cache">
-
<meta http-equiv="cache-control" content="no-cache">
-
<meta http-equiv="expires" content="0">
-
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
-
<meta http-equiv="description" content="This is my page">
-
-
-
-
-
</head>
-
-
<body>
-
-
<%
- // Create image
- String username,password,url;
-
Connection conn = null;
-
Statement stmt = null;
-
BufferedInputStream inputimage=null;
-
username="dzjc";
-
password="dzjc";
-
url ="jdbc:oracle:thin:@192.168.1.134:1521:zhpt";
- Class.forName("oracle.jdbc.driver.OracleDriver");
-
conn=DriverManager.getConnection(url,username,password);
-
stmt=conn.createStatement();
-
-
boolean defaultCommit = conn.getAutoCommit();
- conn.setAutoCommit(false);
-
- try
- {
-
-
ResultSet rs = stmt.executeQuery("SELECT img FROM dzjc_img");
-
- while (rs.next())
- {
-
oracle.sql.BLOB blob = (oracle.sql.BLOB)rs.getBlob("img");
-
inputimage = new BufferedInputStream(blob.getBinaryStream());
- }
- }
- catch (Exception ex)
- {
- System.out.println("blobRead()'s exception"+ex);
- conn.rollback();
- throw ex;
- }
- conn.setAutoCommit(defaultCommit);
-
- // Send back image
-
BufferedImage image = null;
- try
- {
-
image = ImageIO.read(inputimage);
- }
- catch(IOException e)
- {
- System.out.println(e);
- }
-
ServletOutputStream sos = response.getOutputStream();
-
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(sos);
- encoder.encode(image);
- inputimage.close();
- /*/**/
-
%>
-
-
</body>
-
</html:html>
<%@ page language="java" pageEncoding="gbk" contentType="image/jpeg" import="java.awt.image.*,java.sql.*,com.sun.image.codec.jpeg.*,java.util.*,javax.imageio.*,java.io.*"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html lang="true">
<head>
<html:base />
<title>EPhotoDis.jsp</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
// Create image
String username,password,url;
Connection conn = null;
Statement stmt = null;
BufferedInputStream inputimage=null;
username="dzjc";
password="dzjc";
url ="jdbc:oracle:thin:@192.168.1.134:1521:zhpt";
Class.forName("oracle.jdbc.driver.OracleDriver");
conn=DriverManager.getConnection(url,username,password);
stmt=conn.createStatement();
boolean defaultCommit = conn.getAutoCommit();
conn.setAutoCommit(false);
try
{
ResultSet rs = stmt.executeQuery("SELECT img FROM dzjc_img");
while (rs.next())
{
oracle.sql.BLOB blob = (oracle.sql.BLOB)rs.getBlob("img");
inputimage = new BufferedInputStream(blob.getBinaryStream());
}
}
catch (Exception ex)
{
System.out.println("blobRead()'s exception"+ex);
conn.rollback();
throw ex;
}
conn.setAutoCommit(defaultCommit);
// Send back image
BufferedImage image = null;
try
{
image = ImageIO.read(inputimage);
}
catch(IOException e)
{
System.out.println(e);
}
ServletOutputStream sos = response.getOutputStream();
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(sos);
encoder.encode(image);
inputimage.close();
/*/**/
%>
</body>
</html:html>
再来个在action里用的
- package struts.action;
-
-
import java.awt.image.BufferedImage;
-
import java.io.BufferedInputStream;
-
import java.io.IOException;
-
import java.sql.Connection;
-
import java.sql.DriverManager;
-
import java.sql.ResultSet;
-
import java.sql.SQLException;
-
import java.sql.Statement;
-
-
import javax.imageio.ImageIO;
-
import javax.servlet.ServletOutputStream;
-
import javax.servlet.http.HttpServletRequest;
-
import javax.servlet.http.HttpServletResponse;
-
-
import org.apache.struts.action.Action;
-
import org.apache.struts.action.ActionForm;
-
import org.apache.struts.action.ActionForward;
-
import org.apache.struts.action.ActionMapping;
-
-
import com.sun.image.codec.jpeg.JPEGCodec;
-
import com.sun.image.codec.jpeg.JPEGImageEncoder;
-
-
public class EnrolRushRedLightPhotoManagePhotoDisplayAction extends Action
- {
-
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,HttpServletResponse response)
- {
-
- String username,password,url;
-
Connection conn = null;
-
Statement stmt = null;
-
BufferedInputStream inputimage=null;
-
username="dzjc";
-
password="dzjc";
-
url ="jdbc:oracle:thin:@192.168.1.134:1521:zhpt";
-
-
try
- {
-
Class.forName("oracle.jdbc.driver.OracleDriver");
-
try
- {
- conn=DriverManager.getConnection(url,username,password);
- stmt=conn.createStatement();
-
boolean defaultCommit = conn.getAutoCommit();
-
conn.setAutoCommit(false);
-
ResultSet rs = stmt.executeQuery("SELECT img FROM dzjc_img");
-
-
while (rs.next())
- {
-
oracle.sql.BLOB blob = (oracle.sql.BLOB)rs.getBlob("img");
-
inputimage = new BufferedInputStream(blob.getBinaryStream());
- }
- conn.setAutoCommit(defaultCommit);
- }
-
catch (SQLException e)
- {
- e.printStackTrace();
-
System.out.println("blobRead()'s exception"+e);
-
try
- {
- conn.rollback();
-
throw e;
- }
-
catch (SQLException e1)
- {
- e1.printStackTrace();
- }
- }
- }
-
catch (ClassNotFoundException e1)
- {
- e1.printStackTrace();
- }
-
-
-
BufferedImage image = null;
-
try
- {
- image = ImageIO.read(inputimage);
- ServletOutputStream sos = response.getOutputStream();
- JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(sos);
- encoder.encode(image);
- inputimage.close();
- }
-
catch(IOException e)
- {
- System.out.println(e);
- }
-
return mapping.findForward("display");
- }
- }
分享到:
相关推荐
Jupyter-Notebook
Jupyter-Notebook
高效甘特图模板下载-精心整理.zip
lstm Summary Framework: z = U>x, x u Uz Criteria for choosing U: • PCA: maximize projected variance • CCA: maximize projected correlation • FDA: maximize projected intraclass variance
OpenGL调试工具,适合图形开发者,包括视频开发,播放器开始以及游戏开发者。
全国行政区划shp最新图.zip
全国研究生招生与在校数据+国家线-最新.zip
Jupyter-Notebook
直播电商交流平台 SSM毕业设计 附带论文 启动教程:https://www.bilibili.com/video/BV1GK1iYyE2B
《林黛玉进贾府》课本剧剧本
2000-2020年沪深A股上市公司融资约束程度SA指数-最新数据发布.zip
PPT模版资料,PPT模版资料
CPA注会考试最新教材资料-最新发布.zip
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。
内容概要:本文提供了一个完整的职工管理系统的C++源代码。通过面向对象的编程方法,实现了包括创建新职工、查询、增加、修改、删除、排序、统计以及存储和恢复职工数据在内的多个基本操作功能。该系统支持不同的用户角色(如管理员与老板),并通过菜单驱动方式让用户方便地进行相关操作。此外,还包括了错误检测机制,确保操作过程中的异常得到及时处理。 适合人群:有一定C++语言基础,特别是面向对象编程经验的程序员;企业管理人员和技术开发人员。 使用场景及目标:适用于中小型企业内部的人力资源管理部门或IT部门,用于维护员工基本信息数据库,提高工作效率。通过本项目的学习可以加深对链表、类和对象的理解。 阅读建议:建议先熟悉C++的基本语法和面向对象概念,再深入学习代码的具体实现细节。对于关键函数,比如exchange、creatilist等,应当重点关注并动手实践以加强理解。
Jupyter-Notebook
考研公共课历年真题集-最新发布.zip
Huawei-HKUST Joint Workshop on Theory for Future Wireless 15-16 September 2022 华为-香港科技大学未来无线理论联合研讨会 Speaker:Jingwen Tong
演出人员与观众疫情信息管理系统 SSM毕业设计 附带论文 启动教程:https://www.bilibili.com/video/BV1GK1iYyE2B
《林黛玉进贾府》课本剧剧本.pdf