現在的位置: 首頁 > 編程語言 > 正文

java怎樣操作mysql存儲讀取圖片

2020年06月05日 編程語言 ⁄ 共 2158字 ⁄ 字型大小 評論關閉

  為了使用JSP靈活需要把各種文件儲存在資料庫中然後需要的時候將它讀取出來顯示到客戶端這些文件包括文本圖片音樂等人們統稱為二進位文件。下面學步園小編來講解下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="image/jpeg;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";try{   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)   {   e.printStackTrace();   }catch(IOExceptione){   e.printStackTrace();   }   %>

  

  

  以上就是關於「java怎樣操作mysql存儲讀取圖片」的內容,希望對大家有用。更多資訊請關注學步園。學步園,您學習IT技術的優質平台!

抱歉!評論已關閉.