java取oracle和mysql区别数据库中图片

&//一般是先通过select&& ... for update锁定blob列,然后写入blob值,然后提交。要用到特定的Oracle BLOB类。
《--------保存图片到数据库--------------》
public void savePic(InputStream is){
&&&&&&& String insertsql = &insert into pic(id,pic) values(1,empty_blob())&;//首先插入一个空的Blob类型
&&&&&&& Connection conn =
&&&&&&& Statement stmt =
&&&&&&& PreparedStatement ps =
&&&&&&& ResultSet rs =
&&&&&&& try {
&&&&&&&&&&& conn = ds.getConnection();
&&&&&&&&&&& conn.setAutoCommit(false);
&&&&&&&&&&& stmt = conn.createStatement();
&&&&&&&&&&& int rt = stmt.executeUpdate(insertsql);
&&&&&&&&&&& if(rt & 0){
&&&&&&&&&&&&&&& System.out.println(&initialization succeed!!&);
&&&&&&&&&&&&&&& String preCursor = &select id,pic from pic where id='1' for update&;
&&&&&&&&&&&&&&& rs = stmt.executeQuery(preCursor);//锁定列
&&&&&&&&&&&&&&& rs.next();
&&&&&&&&&&&&&&& Blob b = (Blob)rs.getBlob(&pic&);
&&&&&&&&&&&&&&& BufferedOutputStream bos = new BufferedOutputStream(b.setBinaryStream(0L));
&&&&&&&&&&&&&&& BufferedInputStream bis = new BufferedInputStream(is);
&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&& try {
&&&&&&&&&&&&&&&&&&& while((bytes = bis.read()) != -1){
&&&&&&&&&&&&&&&&&&&&&&& bos.write(bytes);
&&&&&&&&&&&&&&&&&&& }
&&&&&&&&&&&&&&&&&&& bis.close();
&&&&&&&&&&&&&&&&&&& bos.close();
&&&&&&&&&&&&&&&&&&& conn.commit();
&&&&&&&&&&&&&&& } catch (IOException e) {
&&&&&&&&&&&&&&&&&&& e.printStackTrace();
&&&&&&&&&&&&&&& }
&&&&&&&&&&& }
&&&&&&& } catch (SQLException e) {
&&&&&&&&&&& e.printStackTrace();
&&&&&&& } finally{
&&&&&&&&&&& try {
&&&&&&&&&&&&&&& rs.close();
&&&&&&&&&&&&&&& stmt.close();
&&&&&&&&&&&&&&& conn.close();
&&&&&&&&&&& } catch (SQLException e) {
&&&&&&&&&&&&&&& e.printStackTrace();
&&&&&&&&&&& }
《--------获取图片到到页面--------------》
public JPEGImageEncoder getPic(String id,HttpServletResponse res) throws IOException{
&&&&&&& Connection conn =
&&&&&&& Statement stmt =
&&&&&&& PreparedStatement ps =
&&&&&&& ResultSet rs =
&&&&&&& BufferedInputStream imageStream =
&&&&&&& BufferedImage image =
&&&&&&& JPEGImageEncoder encoder =
&&&&&&& ServletOutputStream sos =& res.getOutputStream();
&&&&&&& try {
&&&&&&&&&&& conn = ds.getConnection();
&&&&&&&&&&& conn.setAutoCommit(false);
&&&&&&&&&&& stmt = conn.createStatement();
&&&&&&&&&&& System.out.println(&initialization succeed!!&);
&&&&&&&&&&& String preCursor = &select id,pic from pic where id='1'&;
&&&&&&&&&&& rs = stmt.executeQuery(preCursor);
&&&&&&&&&&& rs.next();
&&&&&&&&&&& imageStream = new BufferedInputStream(((Blob)rs.getBlob(2)).getBinaryStream());
&&&&&&&&&&& try {
&&&&&&&&&&&&&&& image = ImageIO.read(imageStream);
&&&&&&&&&&&&&&& encoder =& JPEGCodec.createJPEGEncoder(sos);
&&&&&&&&&&&&&&& encoder.encode(image);
&&&&&&&&&&&&&&& imageStream.close();
&&&&&&&&&&& } catch (IOException e) {
&&&&&&&&&&&&&&& e.printStackTrace();
&&&&&&&&&&& }
&&&&&&& } catch (SQLException e) {
&&&&&&&&&&& e.printStackTrace();
&&&&&&& } finally{
&&&&&&&&&&& try {
&&&&&&&&&&&&&&& rs.close();
&&&&&&&&&&&&&&& stmt.close();
&&&&&&&&&&&&&&& conn.close();
&&&&&&&&&&& } catch (SQLException e) {
&&&&&&&&&&&&&&& e.printStackTrace();
&&&&&&&&&&& }
从数据库获取图片,导出到本地的完整代码:
import java.awt.image.BufferedI
import java.io.BufferedInputS
import java.io.F
import java.io.FileOutputS
import java.io.IOE
import java.io.OutputS
import java.sql.B
import java.sql.C
import java.sql.DriverM
import java.sql.PreparedS
import java.sql.ResultS
import java.sql.SQLE
import java.sql.S
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputS
import javax.servlet.http.HttpServletR
import com.sun.image.codec.jpeg.JPEGC
import com.sun.image.codec.jpeg.JPEGImageE
public class DBManager {
&&& private String url = &jdbc:oracle:thin:@10.23.65.21:1521:orcl&;
&&& private String userName = &test&;
&&& private String password = &test&;
&&& private String driver&& = &oracle.jdbc.driver.OracleDriver&;
&&& private C
&&& public DBManager(){
&&&&&&& try {
&&&&&&&&&&& Class.forName(driver);
&&&&&&&&&&& conn = DriverManager.getConnection(url, userName, password);
&&&&&&& } catch (ClassNotFoundException e) {
&&&&&&&&&&& e.printStackTrace();
&&&&&&& } catch (SQLException e) {
&&&&&&&&&&& e.printStackTrace();
&&& public void getPic(String picPath) throws IOException{
&&&&&&& Statement stmt =
&&&&&&& PreparedStatement ps =
&&&&&&& ResultSet rs =
&&&&&&& BufferedInputStream imageStream =
&&&&&&& BufferedImage image =
&&&&&&& JPEGImageEncoder encoder =
&&&&&&& try {
&&&&&&&&&&& conn.setAutoCommit(false);
&&&&&&&&&&& stmt = conn.createStatement();
&&&&&&&&&&& System.out.println(&initialization succeed!!&);
&&&&&&&&&&& String preCursor = &select id,zp from zpb where id='2220087'&;
&&&&&&&&&&& rs = stmt.executeQuery(preCursor);
&&&&&&&&&&& while(rs.next()){
&&&&&&&&&&&&&&& imageStream = new BufferedInputStream(((Blob)rs.getBlob(2)).getBinaryStream());
&&&&&&&&&&&&&&& try {
&&&&&&&&&&&&&&&&&&& String id = rs.getString(1);
&&&&&&&&&&&&&&&&&&& File picturePath = new File(picPath +&/&+ id +&.jpg&);
&&&&&&&&&&&&&&&&&&& boolean created& =
&&&&&&&&&&&&&&&&&&& if(!picturePath.exists()){
&&&&&&&&&&&&&&&&&&&&&&& created = picturePath.createNewFile();
&&&&&&&&&&&&&&&&&&& } else {
&&&&&&&&&&&&&&&&&&&&&&& created& =
&&&&&&&&&&&&&&&&&&& }
&&&&&&&&&&&&&&&&&&& if(created){
//&&&&&&&&&&&&&&&&&&&&&&& ServletOutputStream sos =& res.getOutputStream();
&&&&&&&&&&&&&&&&&&&&&&& OutputStream ostream = new FileOutputStream(picturePath);
&&&&&&&&&&&&&&&&&&&&&&& image = ImageIO.read(imageStream);
&&&&&&&&&&&&&&&&&&&&&&& encoder =& JPEGCodec.createJPEGEncoder(ostream);
&&&&&&&&&&&&&&&&&&&&&&& encoder.encode(image);
&&&&&&&&&&&&&&&&&&&&&&& imageStream.close();
&&&&&&&&&&&&&&&&&&&&&&& ostream.flush();
&&&&&&&&&&&&&&&&&&&&&&& ostream.close();
&&&&&&&&&&&&&&&&&&& }
&&&&&&&&&&&&&&& } catch (IOException e) {
&&&&&&&&&&&&&&&&&&& e.printStackTrace();
&&&&&&&&&&&&&&& }
&&&&&&&&&&& }
&&&&&&& } catch (SQLException e) {
&&&&&&&&&&& e.printStackTrace();
&&&&&&& } finally{
&&&&&&&&&&& try {
&&&&&&&&&&&&&&& rs.close();
&&&&&&&&&&&&&&& stmt.close();
&&&&&&&&&&&&&&& conn.close();
&&&&&&&&&&& } catch (SQLException e) {
&&&&&&&&&&&&&&& e.printStackTrace();
&&&&&&&&&&& }
&&& public static void main(String[] s){
&&&&&&& DBManager manager = new DBManager();
&&&&&&& String localPath = &E:\testpics&;
&&&&&&& try {
&&&&&&&&&&& manager.getPic(localPath);
&&&&&&& } catch (IOException e) {
&&&&&&&&&&& e.printStackTrace();
//CLOB类专用处理:
public static String getClobString(ResultSet rs, int col) {
Clob c = rs.getClob(2);
Reader reader = c.getCharacterStream();
if (reader == null) {
StringBuffer sb = new StringBuffer();
char[] charbuf = new char[4096];
for (int i = reader.read(charbuf); i & 0; i = reader.read(charbuf)) {
sb.append(charbuf, 0, i);
return sb.toString();
} catch (Exception e) {
return &&;
访问:296919次
积分:4305
排名:第8283名
原创:105篇
转载:101篇
评论:31条博客分类:
&&& 最近做个项目使用S2SH和Oracle数据库,其中photo表使用blob类型存储的图片。在网上搜了很多资料,现在把实例代码发一下。
Photo实体类
public class Photo implements java.io.Serializable {
private String contentT
private Set facelookmarks = new HashSet(0);
private Set facelookactivities = new HashSet(0);
private Set facelookcomments = new HashSet(0);
/*setter,getter省略*/
Photo映射文件
&class name="org.facelook.model.Photo" table="FACELOOKPHOTO" schema="YANGMENG"&
/*其他省略*/
&property name="content" type="java.sql.Blob"&
&column name="CONTENT" /&
&/property&
&property name="thumbnail" type="java.sql.Blob"&
&column name="THUMBNAIL" /&
&/property&
&&& oracle中的blob类型,在实体类中使用java.sql.Blob对应。
从数据库查询数据时正常写查询方法就可以,在页面中输出图片时,在img标签的src属性写要访问的路径,我这里是PhotoAction的thumb方法。
[img]photo_thumb?id=&s:property value='photo.id'/&[/img]
PhotoAction的thumb方法
public String thumb(){
this.photo = this.pService.getById(this.id);
this.contentType = this.photo.getContentType();
this.inputStream = this.photo.getContent().getBinaryStream();
} catch (SQLException e) {
e.printStackTrace();
return "thumb";
struts.xml
&action name="photo_*" class="PhotoAction" method="{1}"&
&result name="thumb" type="stream"&
&param name="contentType"&${contentType}&/param&
&param name="bufferSize"&1024&/param&
&&& 其中contentType是下载类型,读取的图片所以${contentType}值为image/jpeg。&
&&& 这样图片就从数据库中读取出来了。以上是使用struts2的方法。如果不使用struts2来实现也可以使用流输出出来。代码如下:
this.photo = this.pService.getById(this.id);
response.setContentType(this.photo.getContentType());//this.photo.getContentType()值为image/jpeg
ServletOutputStream op = response.getOutputStream();
InputStream is = this.photo.getContent().getBinaryStream();
BufferedInputStream bis = new BufferedInputStream(is);
BufferedImage bi = ImageIO.read(bis);
JPEGImageEncoderImpl jpeg = new JPEGImageEncoderImpl(op);
jpeg.encode(bi);
op.close();
response.flushBuffer();
out.clear();
out = pageContext.pushBody();
yangmeng_3331
浏览: 45229 次
来自: 天津
该用户名已存在 写道如果是字符串,是按照 parse 方法中的 ...
如果是字符串,是按照 parse 方法中的规则进行解析。var ...
shiyiyue513 写道文中有诸多不妥之处。我是初学者,一 ...
文中有诸多不妥之处。
(window.slotbydup=window.slotbydup || []).push({
id: '4773203',
container: s,
size: '200,200',
display: 'inlay-fix'JAVA读取Oracle中的blob图片字段并显示
整个流程分为四步,连接oracle数据库 -& 读取blob图片字段
-& 对图片进行缩放 -&把图片展示在jsp页面上。
下面进行详细描述:
1. java连接Oracle
注:数据库是Oracle10g版本为10.2.0, 在数据库中,图片字段类型为BLOB。
java中通常使用的是通过jdbc驱动来连接数据库,oracle也不例外,因此必须下载一个Oracle驱动的jdbc需要去网上进行下载,名称为
ojdbc14.jar。
下载地址为:
下载了驱动之后,可以使用驱动里提供的接口进行连接,具体代码如下:
import java.sql.*;
import java.io.*;
import javax.imageio.ImageIO;
import java.awt.image.BufferedI
import java.awt.image.AffineTransformOp;
import java.awt.geom.AffineT
public class OracleQueryBean {
&&& private
final String oracleDriverName =
"oracle.jdbc.driver.OracleDriver";
&&& private
Connection myConnection =
&&& private
String strTabN
&&& private
String strIDN
&&& private
String strImgN
&&& public
OracleQueryBean(){
&&&&&&&&&&&
Class.forName(oracleDriverName);
}catch(ClassNotFoundException ex){
&&&&&&&&&&&
System.out.println("加载jdbc驱动失败,原因:" + ex.getMessage());
&&& public
Connection getConnection(){
//用户名+密码; 以下使用的Test就是Oracle里的表空间
//从配置文件中读取数据库信息
GetPara oGetPara = new GetPara();
String strIP = oGetPara.getPara("serverip");
String strPort = oGetPara.getPara("port");
String strDBName = oGetPara.getPara("dbname");
String strUser = oGetPara.getPara("user");
String strPassword = oGetPara.getPara("password");
this.strTabName = oGetPara.getPara("tablename");
this.strIDName = oGetPara.getPara("imgidname");
this.strImgName = oGetPara.getPara("imgname");
String oracleUrlToConnect
="jdbc:oracle:thin:@"+strIP+":"+strPort+":"+strDBN
&&&&&&&&&&&
this.myConnection = DriverManager.getConnection(oracleUrlToConnect,
strUser, strPassword);
}catch(Exception ex){
&&&&&&&&&&&
System.out.println("Can not get connection:" +
ex.getMessage());
&&&&&&&&&&&
System.out.println("请检测配置文件中的数据库信息是否正确." );
return this.myC
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)
this.getConnection();
&&& byte[]
&&&&&&&&&&&
Statement stmt = myConnection.createStatement();
&&&&&&&&&&&
ResultSet myResultSet = stmt.executeQuery("select " +
this.strIDName + " from " + this.strTabName + " where " +
this.strIDName + "=" + strID);
&&&&&&&&&&&
&&&&&&&&&&&
StringBuffer myStringBuffer = new StringBuffer();
&&&&&&&&&&&
if (myResultSet.next()) {
&&&&&&&&&&&&&&&
java.sql.Blob blob = myResultSet.getBlob(this.strImgName);
&&&&&&&&&&&&&&&
InputStream inStream = blob.getBinaryStream();
&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&
long nLen = blob.length();
&&&&&&&&&&&&&&&&&&&
int nSize = (int) nL
&&&&&&&&&&&&&&&&&&&
//System.out.println("img data size is :" + nSize);
&&&&&&&&&&&&&&&&&&&
data = new byte[nSize];
&&&&&&&&&&&&&&&&&&&
inStream.read(data);
&&&&&&&&&&&&&&&&&&&
inStream.close();
&&&&&&&&&&&&&&&
} catch (IOException e) {
&&&&&&&&&&&&&&&&&&&
System.out.println("获取图片数据失败,原因:" + e.getMessage());
&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&
data = ChangeImgSize(data, w, h);
&&&&&&&&&&&
&&&&&&&&&&&
System.out.println(myStringBuffer.toString());
&&&&&&&&&&&
myConnection.commit();
&&&&&&&&&&&
myConnection.close();
} catch (SQLException ex) {
&&&&&&&&&&&
System.out.println(ex.getMessage());
3. 缩放图片
因为图片的大小可能不一致,但是在页面中输出的大小需要统一,所以需要
在OracleQueryBean类中增加一个函数,来进行缩放,具体代码如下:
&&& private
byte[] ChangeImgSize(byte[] data, int nw, int nh){
&&& byte[]
BufferedImage bis = ImageIO.read(new
ByteArrayInputStream(data));
&&&&&&&&&&&
int w = bis.getWidth();
&&&&&&&&&&&
int h = bis.getHeight();
&&&&&&&&&&&
double sx = (double) nw /
&&&&&&&&&&&
double sy = (double) nh /
&&&&&&&&&&&
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 = baos.toByteArray();
&&&&&&&&&&&
}catch(IOException e){
e.printStackTrace();
&&& return
4. 展示在页面
页面使用OracleQueryBean来根据用户提供的图片id进行查询,在读取并进行缩放后,通过jsp页面进行展示,具体代码如下:
&%@ page language="java"
contentType="text/;charset=gbk" %&
&jsp:useBean id="OrcleQuery" scope="page"
class="HLFtiDemo.OracleQueryBean" /&
response.setContentType("image/jpeg");
//图片在数据库中的 ID
&&& String
strID = request.getParameter("id");
//要缩略或放大图片的宽度
&&& String
strWidth = request.getParameter("w");
//要缩略或放大图片的高度
&&& String
strHeight = request.getParameter("h");
&&& byte[]
&&& if(strID
int nWith = Integer.parseInt(strWidth);
int nHeight = Integer.parseInt(strHeight);
//获取图片的byte数据
data = OrcleQuery.GetImgByteById(strID, nWith, nHeight);
ServletOutputStream op =
response.getOutputStream();&&&&&&&
op.write(data, 0, data.length);
op.close();
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.BufferedI
import java.awt.image.AffineTransformOp;
import java.awt.geom.AffineT
public class OracleQueryBean {
&&& private
final String oracleDriverName =
"oracle.jdbc.driver.OracleDriver";
&&& private
Connection myConnection =
&&& private
String strTabN
&&& private
String strIDN
&&& private
String strImgN
&&& public
OracleQueryBean(){
&&&&&&&&&&&
Class.forName(oracleDriverName);
}catch(ClassNotFoundException ex){
&&&&&&&&&&&
System.out.println("加载jdbc驱动失败,原因:" + ex.getMessage());
&&& public
Connection getConnection(){
//用户名+密码; 以下使用的Test就是Oracle里的表空间
//从配置文件中读取数据库信息
GetPara oGetPara = new GetPara();
String strIP = oGetPara.getPara("serverip");
String strPort = oGetPara.getPara("port");
String strDBName = oGetPara.getPara("dbname");
String strUser = oGetPara.getPara("user");
String strPassword = oGetPara.getPara("password");
this.strTabName = oGetPara.getPara("tablename");
this.strIDName = oGetPara.getPara("imgidname");
this.strImgName = oGetPara.getPara("imgname");
String oracleUrlToConnect
="jdbc:oracle:thin:@"+strIP+":"+strPort+":"+strDBN
&&&&&&&&&&&
this.myConnection = DriverManager.getConnection(oracleUrlToConnect,
strUser, strPassword);
}catch(Exception ex){
&&&&&&&&&&&
System.out.println("Can not get connection:" +
ex.getMessage());
&&&&&&&&&&&
System.out.println("请检测配置文件中的数据库信息是否正确." );
return this.myC
&&& public
byte[] GetImgByteById(String strID, int w, int h){
//System.out.println("Get img data which id is " + nID);
if(myConnection == null)
this.getConnection();
&&& byte[]
&&&&&&&&&&&
Statement stmt = myConnection.createStatement();
&&&&&&&&&&&
ResultSet myResultSet = stmt.executeQuery("select " +
this.strIDName + " from " + this.strTabName + " where " +
this.strIDName + "=" + strID);
&&&&&&&&&&&
&&&&&&&&&&&
StringBuffer myStringBuffer = new StringBuffer();
&&&&&&&&&&&
if (myResultSet.next()) {
&&&&&&&&&&&&&&&
java.sql.Blob blob = myResultSet.getBlob(this.strImgName);
&&&&&&&&&&&&&&&
InputStream inStream = blob.getBinaryStream();
&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&
long nLen = blob.length();
&&&&&&&&&&&&&&&&&&&
int nSize = (int) nL
&&&&&&&&&&&&&&&&&&&
//System.out.println("img data size is :" + nSize);
&&&&&&&&&&&&&&&&&&&
data = new byte[nSize];
&&&&&&&&&&&&&&&&&&&
inStream.read(data);
&&&&&&&&&&&&&&&&&&&
inStream.close();
&&&&&&&&&&&&&&&
} catch (IOException e) {
&&&&&&&&&&&&&&&&&&&
System.out.println("获取图片数据失败,原因:" + e.getMessage());
&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&
data = ChangeImgSize(data, w, h);
&&&&&&&&&&&
&&&&&&&&&&&
System.out.println(myStringBuffer.toString());
&&&&&&&&&&&
myConnection.commit();
&&&&&&&&&&&
myConnection.close();
} catch (SQLException ex) {
&&&&&&&&&&&
System.out.println(ex.getMessage());
&&& public
byte[] GetImgByteById(String strID){
//System.out.println("Get img data which id is " + nID);
if(myConnection == null)
this.getConnection();
&&& byte[]
&&&&&&&&&&&
Statement stmt = myConnection.createStatement();
&&&&&&&&&&&
ResultSet myResultSet = stmt.executeQuery("select " +
this.strIDName + " from " + this.strTabName + " where " +
this.strIDName + "=" + strID);
&&&&&&&&&&&
&&&&&&&&&&&
StringBuffer myStringBuffer = new StringBuffer();
&&&&&&&&&&&
if (myResultSet.next()) {
&&&&&&&&&&&&&&&
java.sql.Blob blob = myResultSet.getBlob(this.strImgName);
&&&&&&&&&&&&&&&
InputStream inStream = blob.getBinaryStream();
&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&
long nLen = blob.length();
&&&&&&&&&&&&&&&&&&&
int nSize = (int) nL
&&&&&&&&&&&&&&&&&&&
data = new byte[nSize];
&&&&&&&&&&&&&&&&&&&
inStream.read(data);
&&&&&&&&&&&&&&&&&&&
inStream.close();
&&&&&&&&&&&&&&&
} catch (IOException e) {
&&&&&&&&&&&&&&&&&&&
System.out.println("获取图片数据失败,原因:" + e.getMessage());
&&&&&&&&&&&&&&&
&&&&&&&&&&&
&&&&&&&&&&&
System.out.println(myStringBuffer.toString());
&&&&&&&&&&&
myConnection.commit();
&&&&&&&&&&&
myConnection.close();
} catch (SQLException ex) {
&&&&&&&&&&&
System.out.println(ex.getMessage());
&&& private
byte[] ChangeImgSize(byte[] data, int nw, int nh){
&&& byte[]
BufferedImage bis = ImageIO.read(new
ByteArrayInputStream(data));
&&&&&&&&&&&
int w = bis.getWidth();
&&&&&&&&&&&
int h = bis.getHeight();
&&&&&&&&&&&
double sx = (double) nw /
&&&&&&&&&&&
double sy = (double) nh /
&&&&&&&&&&&
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 = baos.toByteArray();
}catch(IOException e){
e.printStackTrace();
&&& return
已投稿到:
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。java向oracle中存图片
最近开始进入java,上手就碰到这个问题,在csdn上研究了两天终于全部调试成功,现在帖出来希望对象我一样的新手有点帮助
图片存入数据库:
import & java.sql.*;
import & java.io.*;
public & static & void
& main(String[] &
Class.forName(
"com.microsoft.jdbc.sqlserver.SQLServerDriver ");
& Connection & conn
DriverManager.getConnection(
"jdbc:microsoft:sqlserver://JAYQIAN:1433;DataBaseName=CMM;User=Password=jay
& & & File
& file & = & new
& File( "E: ");
& & int & length
& = & (int) &
file.length();
& & InputStream
& input & = &
new & FileInputStream(file);
& & java.sql.PreparedStatement
& preStmt & = &
conn.prepareStatement(
& & "INSERT &
INTO & mytest & (myname,myfile)
& VALUES(?,?) ");
& & preStmt.setString(1,
& "qianwy1 ");
& & preStmt.setBinaryStream(2,
& input, & length);
& & preStmt.execute();
catch & (Exception &
ex.printStackTrace();
这是最简单的将一个服务器上的图片存入数据库,如果您调试这个class可以成功的话,修改放到jsp里也没什么问题.但是如果是客户端的话先要上传文件到服务器,然后
在存到数据库中,上传文件smartupload我就不说了
显示图片的servlet
import & javax.servlet.*;
import & javax.servlet.http.*;
import & java.io.*;
import & java.util.*;
import & java.sql.*;
public & class & getphoto
& extends & HttpServlet
private & static & final
& String & CONTENT_TYPE
& = & "image/jpeg
public & void & init()
& throws & ServletException
public & void &
doGet(HttpServletRequest & request,
& HttpServletResponse & response)
& throws & ServletException,
& IOException &
//在数据库中照片的ID
String & PHOTOID & =
PHOTOID & = &
request.getParameter( "photoid ");
catch(Exception & e) &
e.printStackTrace();
ResultSet & RS_photo & =
Class.forName(
"com.microsoft.jdbc.sqlserver.SQLServerDriver ");
Connection & conn & =
& DriverManager.getConnection(
"jdbc:microsoft:sqlserver://yangxf:1433;DataBaseName=CMM;User=Password=
String & sQuery & =
& "SELECT & photo
& FROM & employee
& WHERE & id=?
PreparedStatement & pstmt=
pstmt & = &
conn.prepareStatement(sQuery);
pstmt.setString(1, & PHOTOID);
RS_photo & = &
pstmt.executeQuery();
}catch & (Exception &
//用来存储照片数据的缓冲区
byte & [] &
//扩展名可以从数据库得到,这里直接指定为JPEG
String & photoname= "jpeg ";
//根据ID查找照片
//将图片数据读入缓冲区
if & (RS_photo.next()){
RS_photo.getBytes(1);
buf & = & new
& byte[0];
}catch & (Exception &
//finally & {
& //connMgr.freeConnection(
"comdb ", & conn);
//response.setContentType(CONTENT_TYPE);
//告诉浏览器输出的是图片
response.setContentType( "image/
"+photoname);
//图片输出的输出流
OutputStream & out & =
& response.getOutputStream();
//将缓冲区的输入输出到页面
out.write(buf);
//输入完毕,清楚缓冲
out.flush();
public & void & destroy()
根据photoid显示相应的图片,您可以直接servlet/getphoto?photoid=XXX测试是否能用
放到jsp里显示图片
&img & src= "servlet/getphoto?photoid
&%=参数%& "&
以上都是最简单的代码,调试成功后再慢慢合成到您的系统中.
已投稿到:
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。}

我要回帖

更多关于 oracle和mysql区别 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信