strutsで画像表示を実現
strutsは接続プールを通じて画像を特定のページに表示し、javaコードはjspに書かず、すべてクラスに書く実現方法を実現する.
jsp里
EnrolRushRedLightPhotoManagePhotoDisplayAction
EnrolRushRedLightPhotoManagePhotoDisplayForm
EnrolRushRedLightPhotoManagePhotoDisplaySQLBean
EnrolRushRedLightPhotoManagePhotoDisplayPager
EnrolRushRedLightPhotoManagePhotoDisplayPagerVo
//jspに表示するフィールド
黒い髪http://heisetoufa.iteye.com
もし本文に誤りが発見されたら,批判と指摘を歓迎する
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
もし本文に誤りが発見されたら,批判と指摘を歓迎する