在之前的项目中做文件上传的时候比较多。实现文件上传,我们有两种实现思路:第一就是将文件上传到服务器的某一位置,然后在数据库中存储文件的服务器上的位置。第二就是直接把文件存储到数据库中。
图片也是文件的一种。这里我们主要讨论以第二种方式存储到Oracle数据库的图片信息如何显示在jsp页面中。
我在项目使用的struts2框架实现的。现在我先描述一下我的需求,请看图片:在用户基本信息 下显示用户已上传的图片。
jsp代码:
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<!-- <div id="watermark" class="watermark"> -->
<div id="tablelist" class="table-list">
<div class="table-list" id="buttons">
<table width="50%" border="0" align="center" bgcolor="#6C92AD">
<tr bgcolor="#EFF7EE" height="50pt" align="center">
<td colspan="5"><font
style="font-size: 20px; font-weight: bold;">用户基本信息</font></td>
</tr>
<tr bgcolor="#EFF7FF" height="40pt">
<td width="10%" bgcolor="#EFF7FF" align="center" valign="middle">姓名</td>
<td width="30%" bgcolor="#FFFFFF" valign="middle">
${uservo.name}</td>
<td width="10%" bgcolor="#EFF7FF" align="center" valign="middle">身份证号</td>
<td width="30%" bgcolor="#FFFFFF" valign="middle">
${uservo.idno}</td>
<td width="20%" bgcolor="#FFFFFF" rowspan="3" valign="middle"><img src="scorerecord!getImage.action?idNo=${uservo.idno}"></img></td>
</tr>
<tr bgcolor="#EFF7FF" height="40pt">
<td width="10%" bgcolor="#EFF7FF" align="center" valign="middle">性别</td>
<td width="30%" bgcolor="#FFFFFF" valign="middle">
${uservo.sex}</td>
<td width="10%" bgcolor="#EFF7FF" align="center" valign="middle">职称</td>
<td width="30%" bgcolor="#FFFFFF" valign="middle">
${uservo.zhicheng}</td>
</tr>
<tr bgcolor="#EFF7FF" height="40pt">
<td width="10%" bgcolor="#EFF7FF" align="center" valign="middle">所属单位</td>
<td width="30%" bgcolor="#FFFFFF" valign="middle">
${uservo.danwei}</td>
<td width="10%" bgcolor="#EFF7FF" align="center" valign="middle">岗位</td>
<td width="30%" bgcolor="#FFFFFF" valign="middle">
${uservo.gangwei}</td>
</tr>
</table>
</div>
</div>
</body>
</html>
主要的就是这一行代码:<img src="scorerecord!getImage.action?idNo=${uservo.idno}"></img>
src指向一个action
action代码:
/**UserExamInfo.java代码
* 获取图片
* @param userExamInfo
*/
public String getImage(){
List<UserExamInfo> userExamList = userExamManager.findByIdNo(idNo);
UserExamInfo userExamInfo = userExamList.get(0); //获取一个实体,实体中content字段存储的是图片信息
Blob photo = userExamInfo.getCONTENT();
HttpServletResponse response = null;
ServletOutputStream sout = null;
try {
response = ServletActionContext.getResponse();
response.setContentType("multipart/form-data");
InputStream in = photo.getBinaryStream();
sout = response.getOutputStream();
byte b[] = new byte[1024];
int len = in.read(b);
while(len!=-1){
sout.write(b);
len = in.read(b);
}
sout.flush();
sout.close();
in.close();
response.flushBuffer();
} catch (SQLException e) {
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}
return null;
}
package com.mongodb.integrity.entity;后台代码通过身份证号查询和我们平时做得一样。
import java.sql.Blob;
import java.util.Date;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="CERTIFICATEINFO")
public class UserExamInfo {
private String NAME; //姓名
private String IDENTITYID; //身份证号
private Blob CONTENT; //照片
@Id
private String CERTIFNO; //证书编号
private String EXAMINEEDEP; //所属单位
private String CERTIFNAME; //证书名称
private String ROLEINFO; //岗位
private Date CREATETIME; //证书生效时间
private String CLOSINGDATE; //证书失效时间
private String EXAMDATE; //考试时间
public String getNAME() {
return NAME;
}
public void setNAME(String nAME) {
NAME = nAME;
}
public String getIDENTITYID() {
return IDENTITYID;
}
public void setIDENTITYID(String iDENTITYID) {
IDENTITYID = iDENTITYID;
}
public Blob getCONTENT() {
return CONTENT;
}
public void setCONTENT(Blob cONTENT) {
CONTENT = cONTENT;
}
public String getCERTIFNO() {
return CERTIFNO;
}
public void setCERTIFNO(String cERTIFNO) {
CERTIFNO = cERTIFNO;
}
public String getEXAMINEEDEP() {
return EXAMINEEDEP;
}
public void setEXAMINEEDEP(String eXAMINEEDEP) {
EXAMINEEDEP = eXAMINEEDEP;
}
public String getCERTIFNAME() {
return CERTIFNAME;
}
public void setCERTIFNAME(String cERTIFNAME) {
CERTIFNAME = cERTIFNAME;
}
public String getROLEINFO() {
return ROLEINFO;
}
public void setROLEINFO(String rOLEINFO) {
ROLEINFO = rOLEINFO;
}
public Date getCREATETIME() {
return CREATETIME;
}
public void setCREATETIME(Date cREATETIME) {
CREATETIME = cREATETIME;
}
public String getCLOSINGDATE() {
return CLOSINGDATE;
}
public void setCLOSINGDATE(String cLOSINGDATE) {
CLOSINGDATE = cLOSINGDATE;
}
public String getEXAMDATE() {
return EXAMDATE;
}
public void setEXAMDATE(String eXAMDATE) {
EXAMDATE = eXAMDATE;
}
}