快上网专注成都网站设计 成都网站制作 成都网站建设
成都网站建设公司服务热线:028-86922220

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

java中怎么利用mysql存储读取图片-创新互联

这篇文章将为大家详细讲解有关java中怎么利用mysql存储读取图片,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

专注于为中小企业提供成都网站设计、网站建设服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业岢岚免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了1000多家企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。

java怎样操作mysql存储读取图片

1在mysql中创建一个表picture_db

createtablepicture_db(

file_namevarchar(255)notnull,

contentlongblob,

primarykey(file_name));

2java写储存文件的代码

importjava.sql.*;

importjava.io.*;

importjava.nio.*;

publicclassUploadImage{

protectedConnectiondbConnection;

protectedStringdriverName="com.mysql.jdbc.Driver";

protectedStringdbURL="jdbc:mysql://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){

e.printStackTrace();

}

returnfalse;

}

privatebooleansetImage(Stringsqlstr,byte[]in){

booleanflag=false;

if(sqlstr==null)

sqlstr="select*frompicture_db";

try{

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.updateBytes(2,in);

rs.insertRow();

}

rs.close();

flag=true;

}catch(Exceptione){

e.printStackTrace();

}

returnflag;

}

publicstaticvoidmain(String[]args){

UploadImageupload=newUploadImage();

try{

Filefile=newFile("01.jpg");

if(upload.storeImage(null,file))

System.out.print("ture");

else

System.out.print("False");

}catch(Exceptione){

e.printStackTrace();

}

}

}

如果执行成功的话系统打印true否则false

java怎样操作mysql存储读取图片

3就是将图片读取出来与储存的过程相反先建立连接创建数据库查询JDBC对象使用该语句来返回二进制结果保存到文件中

<%@pagecontenttype= charset="GB2312"%">

<%@pageimport="java.sql.*"%><%@pageimport="java.io.*"%>

<%@pageimport="com.sun.image.codec.jpeg.*"%>

<%@pageimport="javax.imageio.*"%>

<%@pageimport="java.awt.image.*"%> 

<% stringshowimage="select*frompicture_dbwherefile_name='01'" connectionconn="null;" bufferedinputstreaminputimage="null;StringdriverName="com.mysql.jdbc.Driver";" stringdburl="jdbc:mysql://localhost:3306/sample_db" stringuserid="root" stringpasswd="yourpassword" conn="DriverManager.getConnection(dbURL,userID,passwd);Statementst=conn.createStatement();" resultsetrs="st.executeQuery(showImage);" blobblob="(Blob)rs.getBlob("content");" inputimage="newBufferedInputStream(blob.getBinaryStream());" bufferedimageimage="null;" image="ImageIO.read(inputImage);" servletoutputstreamsos="response.getOutputStream();" jpegimageencoderencoder="JPEGCodec.createJPEGEncoder(sos);">

关于java中怎么利用mysql存储读取图片就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。


新闻标题:java中怎么利用mysql存储读取图片-创新互联
链接地址:http://6mz.cn/article/eoodg.html

其他资讯