java中怎么利用mysql存储读取图片

发布时间:2021-07-27 17:03:59 作者:Leah
来源:亿速云 阅读:260

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

  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存储读取图片就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

推荐阅读:
  1. 如何在MySQL中存储图片
  2. 怎么在MySQL中存储文本和图片

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

java mysql

上一篇:Java中怎么利用switch实现多值匹配操作

下一篇:Javascript中怎么对iframe进行操作

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》