MYSQL存取图片文件Word格式.docx

上传人:b****5 文档编号:16958514 上传时间:2022-11-27 格式:DOCX 页数:10 大小:19KB
下载 相关 举报
MYSQL存取图片文件Word格式.docx_第1页
第1页 / 共10页
MYSQL存取图片文件Word格式.docx_第2页
第2页 / 共10页
MYSQL存取图片文件Word格式.docx_第3页
第3页 / 共10页
MYSQL存取图片文件Word格式.docx_第4页
第4页 / 共10页
MYSQL存取图片文件Word格式.docx_第5页
第5页 / 共10页
点击查看更多>>
下载资源
资源描述

MYSQL存取图片文件Word格式.docx

《MYSQL存取图片文件Word格式.docx》由会员分享,可在线阅读,更多相关《MYSQL存取图片文件Word格式.docx(10页珍藏版)》请在冰豆网上搜索。

MYSQL存取图片文件Word格式.docx

mysql:

//localhost/img?

user=root&

password=root&

useUnicode=true&

characterEncoding=gbk"

;

Connectionconn=DriverManager.getConnection(url);

PreparedStatementpstmt="

INSERTINTOmyTable(image)VALUES(?

)"

pstmt=conn.prepareStatement(sql);

Filefile=newFile("

file的URL"

InputStreamimageStream=newFileInputStream(file);

pstmt.setBinaryStream(1,imageStream,(int)file.length());

pstmt.executeUpdate();

pstmt.close();

conn.close();

取:

PreparedStatementpstmt=conn.prepareStatement("

selectimagefrommyTablewhereid=?

"

//设置ID 

byte[]buffer=newbyte[4096];

Filefile=newFile(outfile);

//读取后要保存的位置 

FileOutputStreamfos=newFileOutputStream(file);

ResultSetrs=pstmt.executeQuery();

InputStreamin=rs.getBinaryStream("

image"

intsize=-1;

while((size=is.read(buffer))!

=-1){ 

fos.write(buffer,0,size);

//如果把fos改成是servlet中的response.getOutputStream(),就可以将图片返回给客户端显示了。

//关闭资源 

二、

JSP在MySQL中存取图片

2008年04月04日星期五23:

41

为了使用JSP灵活,需要把各种文件储存到数据库中,然后需要的时候把它读取出来显示到客户端。

这些文件包括音乐,图片,文本等,人们统称为二进制文件。

首先,二进制文件储存到数据库的过程:

打开文件,将内容读到缓冲区,然后直接联线创建jdbc语句对象,使用该缓冲区的数据,并执行更新,就完成了储存。

例子:

首先在mysql中创建一个表picture_db

createtablepicture_db(

file_namevarchar(255)notnull,

contentlongblob,

primarykey(file_name));

接下来就是用java写储存文件的代码:

假设要储存的图片名称是:

01.jpg(放在同一个目录下)

importjava.sql.*;

importjava.io.*;

importjava.nio.*;

publicclassUploadImage{

protectedConnectiondbConnection;

protectedStringdriverName="

com.mysql.jdbc.Driver"

protectedStringdbURL="

//localhost:

3306/sample_db"

protectedStringuserID="

root"

protectedStringpasswd="

yourpassword"

publicbooleanstoreImage(Stringsqlstr,Filefile){

try{

FileInputStreamfin=newFileInputStream(file);

ByteBuffernbf=ByteBuffer.allocate((int)file.length());

byte[]array=newbyte[1024];

intoffset=0,length=0;

while((length=fin.read(array))>

0){

if(length!

=1024)

nbf.put(array,0,length);

else

nbf.put(array);

offset+=length;

}

fin.close();

byte[]content=nbf.array();

returnsetImage(sqlstr,content);

}catch(FileNotFoundExceptione){

e.printStackTrace();

}catch(IOExceptione){

returnfalse;

privatebooleansetImage(Stringsqlstr,byte[]in){

booleanflag=false;

if(sqlstr==null)

sqlstr="

select*frompicture_db"

Class.forName(driverName);

dbConnection=DriverManager.getConnection(dbURL,userID,passwd);

Statementstmt=dbConnection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);

ResultSetrs=stmt.executeQuery(sqlstr);

if(rs.next()){

rs.updateBytes(2,in);

rs.updateRow();

else{

rs.moveToInsertRow();

rs.updateString(1,"

01"

rs.insertRow();

rs.close();

flag=true;

}catch(Exceptione){

returnflag;

publicstaticvoidmain(String[]args){

UploadImageupload=newUploadImage();

01.jpg"

if(upload.storeImage(null,file))

System.out.print("

ture"

False"

如果执行成功的话,系统打印“true"

否则“false"

.

最后就是将图片度取出来:

与储存的过程相反,它是县建立连接,创建数据库查询的jdbc对象,使用该语句来返回二进制结果,保存到文件当中。

showImage.jsp

<

%@pagecontentType="

image/jpeg;

charset=GB2312"

%>

%@pageimport="

java.sql.*"

java.io.*"

com.sun.image.codec.jpeg.*"

javax.imageio.*"

java.awt.image.*"

html>

head>

metahttp-equiv="

Content-Type"

content="

>

title>

showDBImage<

/title>

/head>

body>

%

StringshowImage="

select*frompicture_dbwherefile_name='

01'

Connectionconn=null;

BufferedInputStreaminputImage=null;

StringdriverName="

StringdbURL="

StringuserID="

Stringpasswd="

Class.forName(driverName).newInstance();

conn=DriverManager.getConnection(dbURL,userID,passwd);

Statementst=conn.createStatement();

ResultSetrs=st.executeQuery(showImage);

while(rs.next()){

Blobblob=(Blob)rs.getBlob("

content"

inputImage=newBufferedInputStream(blob.getBinaryStream());

BufferedImageimage=null;

image=ImageIO.read(inputImage);

ServletOutputStreamsos=response.getOutputStream();

JPEGImageEncoderencoder=JPEGCodec.createJPEGEncoder(sos);

encoder.encode(image);

inputImage.close();

}catch(SQLExceptione)

{

}catch(IOExceptione){

/body>

/html>

三、

/*

---------------表结构------------

表名:

student2

+--------+-------------+------+-----+---------+-------+

|Field|Type|Null|Key|Default|Extra|

|id|int(4)|NO|PRI|NULL||

|name|varchar(20)|YES||NULL||

|stupic|blob|YES||NULL||

*/

packagecom.ibm.jdbc;

publicclassStoreBLOB{

publicstaticvoidmain(String[]args){

//连接MySQl数据库

Connectioncon=DBManager.getConnection();

PreparedStatementps=null;

InputStreamin=null;

try{

//从本地硬盘读取一张读片

in=newFileInputStream("

d:

/111.jpg"

ps=con.prepareStatement("

insertintostudent2values(?

?

ps.setInt(1,2);

ps.setString(2,"

Tom"

ps.setBinaryStream(3,in,in.available());

ps.executeUpdate();

}catch(IOExceptione){

//TODOAuto-generatedcatchblock

e.printStackTrace();

}catch(SQLExceptione){

//TODOAuto-generatedcatchblock

e.printStackTrace();

}

finally

{

try{

//关闭流

if(in!

=null)in.close();

}catch(IOExceptione){

//关闭相关连接

DBManager.close(ps,con);

}

}

取出操作

publicclassGetBLOB{

publicstaticvoidmain(String[]args){

Connectioncon=DBManager.getConnection();

Statementst=null;

ResultSetrs=null;

InputStreamin=null;

OutputStreamout=null;

try{

st=con.createStatement();

rs=st.executeQuery("

selectstupicfromstudent2whereid=2"

rs.next();

//将光标指向第一行

//从rs中读取stupic放进InputStream对象中

in=rs.getBinaryStream("

stupic"

//申明byte数组,用来存放图片流

byte[]b=newbyte[40000];

in.read(b);

//从InputStream对象中读取数据放进byte数组中

//实例化OutputStream对象,在D盘创建一个图片文件

out=newFileOutputStream("

/222.jpg"

//将文件输出,内容则为byte数组里面的数据

out.write(b);

out.flush();

}catch(SQLExceptione){

//TODOAuto-generatedcatchblock

}

catch(IOExceptione){

finally

{

if(in!

=null)

in.close();

if(out!

out.close();

}catch(IOExceptione){

DBManager.close(rs,st,con);

//关闭相关连接

}

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 成人教育 > 专升本

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1