java中怎么利用mysql存储读取图片-古蔺大橙子建站
RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
java中怎么利用mysql存储读取图片

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

10余年的尧都网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。网络营销推广的优势是能够根据用户设备显示端的尺寸不同,自动调整尧都建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。成都创新互联从事“尧都网站设计”,“尧都网站推广”以来,每个客户项目都认真落实执行。

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://scgulin.cn/article/ieghsg.html