第⼀种⽅法:
春节祝福语2014⼤致⽅法就是,从数据库中读出Blob的流来,写到页⾯中去:
复制代码代码如下:
Connection conn = Connection();
String sql = "SELECT picture FROM teacher WHERE id=1";
PreparedStatement ps = null;
ResultSet rs = null;
InputStream is = null;
OutputStream os = null;
try {
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
()){
is = rs.getBinaryStream(1);
}
response.setContentType("text/html");
os = OutputStream();
int num;
byte buf[] = new byte[1024];
while( (ad(buf))!=-1 ){
os.write(buf, 0, num);
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
is.close();
os.close();
rs.close();
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
在页⾯中:
复制代码代码如下:
<%杨童舒图片
String path = ContextPath();
String basePath = Scheme()+"://"+ServerName()+":"+ServerPort()+path+"/"; %>
<img name="pic" src="<%=basePath+"servlet/DownloadAsStream"%>"/>
搞定。
第⼆种⽅法:
整个流程分为四步,连接oracle数据库 -> 读取blob图⽚字段 -> 对图⽚进⾏缩放 ->把图⽚展⽰在jsp页⾯上。
复制代码代码如下:
import java.sql.*;
import java.io.*;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.awt.image.AffineTransformOp;
import AffineTransform;
public class OracleQueryBean {
private final String oracleDriverName = "oracle.jdbc.driver.OracleDriver";
private Connection myConnection = null;
private String strTabName;
private String strIDName;
private String strImgName;
public OracleQueryBean(){
try{
Class.forName(oracleDriverName);
}catch(ClassNotFoundException ex){
System.out.println("加载jdbc驱动失败,原因:" + ex.getMessage());
}
}
public Connection getConnection(){
try{
/
/⽤户名+密码; 以下使⽤的Test就是Oracle⾥的表空间
//从配置⽂件中读取数据库信息
GetPara oGetPara = new GetPara();
String strIP = Para("serverip");
String strPort = Para("port");
String strDBName = Para("dbname");
String strUser = Para("user");
String strPassword = Para("password");
this.strTabName = Para("tablename");
this.strIDName = Para("imgidname");
this.strImgName = Para("imgname");
String oracleUrlToConnect ="jdbc:oracle:thin:@"+strIP+":"+strPort+":"+strDBName;
System.out.println("Can not get connection:" + ex.getMessage());
System.out.println("请检测配置⽂件中的数据库信息是否正确." );
}
Connection;
}
}
2. 读取blob字段
在OracleQueryBean类中增加⼀个函数,来进⾏读取,具体代码如下:
复制代码代码如下:
public byte[] GetImgByteById(String strID, int w, int h){
//System.out.println("Get img data which id is " + nID);
if(myConnection == null)
byte[] data = null;
try {
Statement stmt = ateStatement();
ResultSet myResultSet = uteQuery("select " + this.strIDName + " from " + this.strTabName + " where " + this.strIDName + "=" + strID);
StringBuffer myStringBuffer = new StringBuffer();
if (()) {
java.sql.Blob blob = Blob(this.strImgName);
InputStream inStream = BinaryStream();
try {
long nLen = blob.length();
int nSize = (int) nLen;
//System.out.println("img data size is :" + nSize);
data = new byte[nSize];
inStream.close();
} catch (IOException e) {
钢结构一平方多少钱System.out.println("获取图⽚数据失败,原因:" + e.getMessage());
}
data = ChangeImgSize(data, w, h);
}
System.out.String());
myConnectionmit();
myConnection.close();
} catch (SQLException ex) {
System.out.Message());
}
return data;
}
3. 缩放图⽚
因为图⽚的⼤⼩可能不⼀致,但是在页⾯中输出的⼤⼩需要统⼀,所以需要
在OracleQueryBean类中增加⼀个函数,来进⾏缩放,具体代码如下:
复制代码代码如下:
private byte[] ChangeImgSize(byte[] data, int nw, int nh){
byte[] newdata = null;
try{
BufferedImage bis = ad(new ByteArrayInputStream(data));
int w = Width();
int h = Height();
double sx = (double) nw / w;
double sy = (double) nh / h;
AffineTransform transform = new AffineTransform();
transform.setToScale(sx, sy);
AffineTransformOp ato = new AffineTransformOp(transform, null);
//原始颜⾊
BufferedImage bid = new BufferedImage(nw, nh, BufferedImage.TYPE_3BYTE_BGR);
ato.filter(bis, bid);
//转换成byte字节
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(bid, "jpeg", baos);
newdata = ByteArray();
}catch(IOException e){
e.printStackTrace();
}
return newdata;
}
4. 展⽰在页⾯
页⾯使⽤OracleQueryBean来根据⽤户提供的图⽚id进⾏查询,在读取并进⾏缩放后,通过jsp页⾯进⾏展⽰,具体代码如下:
复制代码代码如下:
<%@ page language="java" contentType="text/html;;charset=gbk" %>
<jsp:useBean id="OrcleQuery" scope="page" class="HLFtiDemo.OracleQueryBean" />
<%
response.setContentType("image/jpeg");
//图⽚在数据库中的 ID
String strID = Parameter("id");
//要缩略或放⼤图⽚的宽度
String strWidth = Parameter("w");
//要缩略或放⼤图⽚的⾼度
String strHeight = Parameter("h");
byte[] data = null;孔令辉的妻子杨舒婷
if(strID != null){
int nWith = Integer.parseInt(strWidth);
int nHeight = Integer.parseInt(strHeight);
//获取图⽚的byte数据
data = OrcleQuery.GetImgByteById(strID, nWith, nHeight);
ServletOutputStream op = OutputStream();
op.write(data, 0, data.length);
op.close();
op = null;
response.flushBuffer();
//清除输出流,防⽌释放时被捕获异常
out.clear();
out = pageContext.pushBody();
}
%>
5. OracleQueryBean查询类的整体代码
OracleQueryBean.java⽂件代码如下所⽰:
复制代码代码如下:
import java.sql.*;
import java.io.*;
读取配置文件失败import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.awt.image.AffineTransformOp;
import AffineTransform;
public class OracleQueryBean {
private final String oracleDriverName = "oracle.jdbc.driver.OracleDriver";
private Connection myConnection = null;
private String strTabName;
private String strIDName;
private String strImgName;
public OracleQueryBean(){
try{
Class.forName(oracleDriverName);
}catch(ClassNotFoundException ex){
System.out.println("加载jdbc驱动失败,原因:" + ex.getMessage());
}
}
public Connection getConnection(){
try{
//⽤户名+密码; 以下使⽤的Test就是Oracle⾥的表空间
//从配置⽂件中读取数据库信息
GetPara oGetPara = new GetPara();
String strIP = Para("serverip");
String strPort = Para("port");
String strDBName = Para("dbname");
String strUser = Para("user");
String strPassword = Para("password");
this.strTabName = Para("tablename");
this.strIDName = Para("imgidname");
this.strImgName = Para("imgname");
String oracleUrlToConnect ="jdbc:oracle:thin:@"+strIP+":"+strPort+":"+strDBName;
}catch(Exception ex){
System.out.println("Can not get connection:" + ex.getMessage());
System.out.println("请检测配置⽂件中的数据库信息是否正确." );
}
Connection;
}
public byte[] GetImgByteById(String strID, int w, int h){
/
/System.out.println("Get img data which id is " + nID);
if(myConnection == null)
byte[] data = null;
try {
Statement stmt = ateStatement();
ResultSet myResultSet = uteQuery("select " + this.strIDName + " from " + this.strTabName + " where " + this.strIDName + "=" + strID);
StringBuffer myStringBuffer = new StringBuffer();
if (()) {
java.sql.Blob blob = Blob(this.strImgName);
InputStream inStream = BinaryStream();马思纯妈妈
try {
long nLen = blob.length();
int nSize = (int) nLen;
//System.out.println("img data size is :" + nSize);
data = new byte[nSize];
inStream.close();
} catch (IOException e) {
System.out.println("获取图⽚数据失败,原因:" + e.getMessage());
}
data = ChangeImgSize(data, w, h);
}
System.out.String());
myConnectionmit();
myConnection.close();
} catch (SQLException ex) {
System.out.Message());
}
return data;
}
发布评论