把图片从oracle数据库的blob字段中取出来,显示在浏览器上

上两篇文章我发的一个是把图片从硬盘存到数据库里,和把图片从数据库里取出来存在硬盘上,这次再发个把图片从数据库里取出来直接显示在浏览器上,而不保存在硬盘上

先贴个在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">
 <!--
 <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) 
 {
//   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";
  
  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();
  }

  // Send back image
  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");
 }
}

 

黑色头发  http://heisetoufa.iteye.com

你可能感兴趣的:(java,oracle,sql,struts,浏览器)