struts实现图片显示

struts通过连接池实现图片显示在特定的页面,java代码不写在jsp中,全部写在类里的实现方法

jsp里

<%
<img src="EnrolRushRedLightPhotoManagePhotoDisplay.do"></img>
%>

 EnrolRushRedLightPhotoManagePhotoDisplayAction

package struts.action;

import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.sql.Blob;
import java.sql.Connection;
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 javax.servlet.http.HttpSession;

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;

import dbc.DataBaseConnection;

public class EnrolRushRedLightPhotoManagePhotoDisplayAction extends Action 
{
        public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,HttpServletResponse response) 
 {
        Connection conn = null;
  Statement stmt = null;
  BufferedInputStream inputimage=null;
  DataBaseConnection dbc = new DataBaseConnection() ;
  HttpSession session = request.getSession();
  ResultSet rs = null ;
  try 
  {
   try 
   {
    conn = dbc.getConnection();
    
    stmt = conn.createStatement();
    
    boolean defaultCommit = conn.getAutoCommit();
    conn.setAutoCommit(false);
    rs = stmt.executeQuery("SELECT img FROM dzjc_img");

    while (rs.next()) 
    {
            Blob blob = (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 (Exception e1)
  {
   e1.printStackTrace();
  }

  BufferedImage image = null;
  try
  {
   image = ImageIO.read(inputimage);
   ServletOutputStream sos = response.getOutputStream();

   ImageIO.write(image, "jpg", sos);
   JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(sos);
   encoder.encode(image);
        inputimage.close();
   sos.flush();
  }
  catch(Exception e)
  {
   System.out.println(e);
  }
  return mapping.findForward("abc");
 }
}

 EnrolRushRedLightPhotoManagePhotoDisplayForm

package struts.form;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;

public class EnrolRushRedLightPhotoManagePhotoDisplayForm extends ActionForm {
 
 private static final long serialVersionUID = 1L;

 private FormFile errorRushRedLightPhotoManagePhotoDisplayPhoto;
 

 private List errors ;

 public void setErrors(List errors)
 {
  this.errors = errors ;
 }
 public List getErrors()
 {
  return this.errors ;
 }

 public ActionErrors validate(ActionMapping mapping,
   HttpServletRequest request) {
  ActionErrors errors = new ActionErrors() ;
  // 判断用户是否登陆,没有的话返回到登陆页面
  if (request.getSession().getAttribute("sessionUserName")==null||"".equals(request.getSession().getAttribute("sessionUserName"))) 
     {
   mapping.findForward("sessionEnded") ;//mapping是转发到页面的,不是提交
     }
  else
  {

  }
  return  errors;
 }

 public void reset(ActionMapping mapping, HttpServletRequest request) {
  
 }
 public FormFile getErrorRushRedLightPhotoManagePhotoDisplayPhoto() {
  return errorRushRedLightPhotoManagePhotoDisplayPhoto;
 }
 public void setErrorRushRedLightPhotoManagePhotoDisplayPhoto(
   FormFile errorRushRedLightPhotoManagePhotoDisplayPhoto) {
  this.errorRushRedLightPhotoManagePhotoDisplayPhoto = errorRushRedLightPhotoManagePhotoDisplayPhoto;
 }
}

 EnrolRushRedLightPhotoManagePhotoDisplaySQLBean

package bean;

import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.imageio.ImageIO;

import util.PageDBConnection;

public class EnrolRushRedLightPhotoManagePhotoDisplaySQLBean 
{
 @SuppressWarnings("unchecked")
 ResultSet rs = null;
 public boolean isChargeStatPagerSQL(String sessionChargeStatPunishmentBegin,String sessionChargeStatPunishmentStop,String sessionChargeStatInvoiceNumberInput,String sessionChargeStatPersonnelInput,String sessionChargeStatProtectListBrandNumberInput,String sessionChargeStatChargeItemInput,String sessionChargeStatHandOverMoneyInput,String sessionChargeStatManageDepartmentInput)
 {
  boolean SQLErrors = false ;
  System.out.println(SQL);
  try
  {
   // 查询记录
   rs = PageDBConnection.getResultSet(SQL);
   if(rs.next())
   {
    SQLErrors = true ;
   }
  }
  catch(Exception e)
  {
   e.printStackTrace();
   
  }
  finally 
  {
   if (rs != null) 
   {
    try 
    {
     rs.close();
    } 
    catch (SQLException e) 
    {
     e.printStackTrace();
    }
   }
  }
  return SQLErrors;
 }
 @SuppressWarnings("unchecked")
    public boolean readEnrolRushRedLightPhotoManagePhotoDisplay() 
 {
     boolean falseOrTrue = false ;
  int amount = 0;
  InputStream in = null;
  BufferedOutputStream out = null;
  int bufferSize = 8000;
 
   System.out.println("没有找到文件!");
   System.out.println("程序非正常结束...");

  try 
  {
   rs = PageDBConnection.getConnection().createStatement().executeQuery("select img from dzjc_img");
   if (rs.next()) 
   {
    oracle.sql.BLOB blob = (oracle.sql.BLOB)rs.getBlob("img");
    in = blob.getBinaryStream();
    BufferedInputStream inputimage=null;
    
    byte[] b = new byte[ (int) bufferSize];
    int count = in.read(b, 0, (int) bufferSize);
    
    while (count != -1) 
    {
     inputimage = new BufferedInputStream(blob.getBinaryStream());
    }
    Connection conn ;
    BufferedImage image = null;
    try
    {
     image = ImageIO.read(inputimage);
    }
    catch(IOException e)
    {
     System.out.println(e);
    }
   }
   else 
   {
    System.out.println("文档:未找到.");
   }
  }
  catch (Exception e) 
  {
   e.printStackTrace();
  }
  finally 
  {
   if (out != null) 
   {
    try 
    {
     out.close();
    }
    catch (Exception ignored) {}
   }
   if (in != null) 
   {
    try 
    {
     in.close();
    }
    catch (Exception ignored) {}
   }
  }
  return falseOrTrue ;
 }
}

 EnrolRushRedLightPhotoManagePhotoDisplayPager

package util;

import java.util.ArrayList;
import java.util.List;

import vo.EnrolRushRedLightPhotoManagePhotoDisplayPagerVo;

public class EnrolRushRedLightPhotoManagePhotoDisplayPager 
{
 private int currentPage=1;//当前页
 private int totalPages=0;//总页数
 private int pageRecorders=10;//每页10行
 private int totalRows=0;//总行数
 private int pageStartRow=0;//每页的起始行
 private int pageEndRow=0;//每页显示的终止数
 private boolean hasNextPage=false;//有没有下一页
 private boolean hasPreviousPage=false;//有没有上一页
 @SuppressWarnings("unused")
 private List list;
 /**
  * 用于初始化,
  * 也就是说,只要你传过来你一个list就可以分页
  * @param list
  */
 public EnrolRushRedLightPhotoManagePhotoDisplayPager(List list)
 {
  this.list=list;
  totalRows=list.size();
  hasPreviousPage=false;
  currentPage=1;
  if(totalRows%pageRecorders==0)
  {
   totalPages=totalRows/pageRecorders;
     
  }
  else
  {
   totalPages=totalRows/pageRecorders+1;
  }
  if(currentPage>=totalPages)
  {
   hasNextPage=false;
  }
  else
  {
   hasNextPage=true;
  }
  if(totalRows<pageRecorders)
  {
   this.pageStartRow=0;
   this.pageEndRow=totalRows;
  }
  else
  {
   this.pageStartRow=0;
   this.pageEndRow=pageRecorders;
  }
  
 }
 public int getCurrentPage() 
 {
  return currentPage;
 }
 public void setCurrentPage(int currentPage) 
 {
  this.currentPage = currentPage;
 }
 public boolean isHasNextPage() 
 {
  return hasNextPage;
 }
 public void setHasNextPage(boolean hasNextPage) 
 {
  this.hasNextPage = hasNextPage;
 }
 public boolean isHasPreviousPage() 
 {
  return hasPreviousPage;
 }
 public void setHasPreviousPage(boolean hasPreviousPage) 
 {
  this.hasPreviousPage = hasPreviousPage;
 }
 public int getPageEndRow() 
 {
  return pageEndRow;
 }
 public void setPageEndRow(int pageEndRow) 
 {
  this.pageEndRow = pageEndRow;
 }
 public int getPageRecorders() 
 {
  return pageRecorders;
 }
 public void setPageRecorders(int pageRecorders) 
 {
  this.pageRecorders = pageRecorders;
 }
 public int getPageStartRow() 
 {
  return pageStartRow;
 }
 public void setPageStartRow(int pageStartRow) 
 {
  this.pageStartRow = pageStartRow;
 }
 public String getTotalPages() 
 {
  return this.toString(totalPages);
 }
 public void setTotalPages(int totalPages) 
 {
  this.totalPages = totalPages;
 }
 public String getTotalRows() 
 {
  return this.toString(totalRows);
 }
 public void setTotalRows(int totalRows) 
 {
  this.totalRows = totalRows;
 }
 public void getFirstPage()
 {
  currentPage=1;
  hasPreviousPage=false;
  if(currentPage>=totalPages)
  {
   hasNextPage=false;
  }else{
   hasNextPage=true;
  }
 }
 public void getNextPage()
 {
  currentPage=currentPage+1;
  if((currentPage-1)>0)
  {
   hasPreviousPage=true;
  }
  else
  {
   hasPreviousPage=false;
  }
  if(currentPage>=totalPages)
  {
   hasNextPage=false;
  }
  else
  {
   hasNextPage=true;
  }
 }
 public void getPreviousPage()
 {
  currentPage=currentPage-1;
  if(currentPage==0){
   currentPage=1;
  }
  if(currentPage>=totalPages)
  {
   hasNextPage=false;
  }
  else
  {
   hasNextPage=true;
  }
  if((currentPage-1)>0)
  {
   hasPreviousPage=true;
  }
  else
  {
   hasPreviousPage=false;
  }
 }
 public void getLastPage()
 {
  currentPage=totalPages;
  if(currentPage==0)
  {
   currentPage=1;
  }
  hasNextPage=false;
  if((currentPage-1)>0)
  {
   hasPreviousPage=true;
  }
  else
  {
   hasPreviousPage=false;
  }
 }
 public void setGotoPage(int gotoPage)
 {
  this.currentPage=gotoPage;
  hasNextPage=true;
  hasPreviousPage=true;
  if(currentPage<=1){
   currentPage=1;
   hasNextPage=true;
   hasPreviousPage=false;
  }
  if(currentPage>=totalPages)
  {
   currentPage=totalPages;
   hasNextPage=false;
   hasPreviousPage=true;
  }
 }
 @SuppressWarnings("unchecked")
 public List getPageList(){
  if(currentPage*pageRecorders<totalRows)
  {
   pageEndRow=currentPage*pageRecorders;
   pageStartRow=pageEndRow-pageRecorders;
   
  }
  else
  {
   pageEndRow=totalRows;
   pageStartRow=pageRecorders*(totalPages-1);
  }
  System.out.println("起始条数"+pageStartRow);
  System.out.println("结束条数"+pageEndRow);
  List templist=new ArrayList();
  int j=0;
  for(int i=pageStartRow;i<pageEndRow;i++)
  {
   EnrolRushRedLightPhotoManagePhotoDisplayPagerVo enrolRushRedLightPhotoManagePhotoDisplayPagerVo=(EnrolRushRedLightPhotoManagePhotoDisplayPagerVo)list.get(i);
   templist.add(j,enrolRushRedLightPhotoManagePhotoDisplayPagerVo);
   j++;
  }
  return templist;
 }
 public String toString(int temp)
 {
  
  return Integer.toString(temp);
 }

}

 EnrolRushRedLightPhotoManagePhotoDisplayPagerVo

//要显示在jsp里的字段

package vo;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import util.PageDBConnection;

 

public class EnrolRushRedLightPhotoManagePhotoDisplayPagerVo 
{
 private static final EnrolRushRedLightPhotoManagePhotoDisplayPagerVo instance = new EnrolRushRedLightPhotoManagePhotoDisplayPagerVo(); 

 private Connection conn = null; 

 private EnrolRushRedLightPhotoManagePhotoDisplayPagerVo() { 

 } 

 public static EnrolRushRedLightPhotoManagePhotoDisplayPagerVo getInstance() { 
 return instance; 
 } 

 private void initConn() { 
// conn = DBAccess.getInstance().getConn();
  conn = PageDBConnection.getConnection();
 } 


 public byte[] getImage(String imgname) { 
 BufferedInputStream ins;//取得BLOB的IO流 
 byte[] bt = null; 

 initConn(); 
 Blob bo = null; 
 PreparedStatement ps = null; 
 ResultSet rs = null; 
 String sql = "select T_IMAGE from IMAGE_LOB where t_id=?"; 
 try { 
 ps = conn.prepareStatement(sql); 
 ps.setString(1, imgname); 
 rs = ps.executeQuery(); 
 if (rs.next()) { 
 bo = rs.getBlob("T_IMAGE"); 

 try { 
 ins = new BufferedInputStream(bo.getBinaryStream()); 
 int bufferSize = (int) bo.length();//取得BLOB的长度 
 bt = new byte[bufferSize]; 
 try { 
 ins.read(bt, 0, bufferSize); 
 } catch (IOException e) { 

 e.printStackTrace(); 
 } 
// 建立字节缓存 
 } catch (SQLException e) { 

 e.printStackTrace(); 
 } 

 } 
 } catch (SQLException e) { 

 e.printStackTrace(); 
 } finally { 
 try { 
 rs.close(); 
 ps.close(); 
 conn.close(); 
 } catch (SQLException e) { 

 e.printStackTrace(); 
 } 
 } 

 return bt; 
 } 
}

 

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

如果发现本文有误,欢迎批评指正

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值