`

把图片以二进制写入mysql或DB2数据库,再读出来。

阅读更多
//把图片以二进制写入数据库
//一次性只能写入或读取确定目录下的单个文件
package write_read;

import java.io.*;
import java.sql.*;

public class PutImg {
public void putimg() {
  try {
  
   String url = "jdbc:mysql://localhost/img?useUnicode=true&characterEncoding=gb2312";
   Class.forName("com.mysql.jdbc.Driver");
   String username="root";
   String password="root";
//   String url = "jdbc:db2://localhost:50000/SJPT";
//   Class.forName("com.ibm.db2.jcc.DB2Driver").newInstance();
//   String username="DB_SJPT";
//   String password="sjpt";
   Connection conn = DriverManager.getConnection(url,username,password);
   System.out.println("测试连接成功");
   Statement stmt = conn.createStatement();
   //stmt.execute("insert   into   imgt (id)   values   (5)");
  
   PreparedStatement pstmt = null;
   String sql = "";
  
//   LinkedList<String> folderList = new LinkedList<String>();
//  folderList.add("1");
//   while(folderList.size()>0){
//    File file=new File((String)folderList.poll());
//    File[] files=file.listFiles();
//   List<File> fileList=new ArrayList<File>();
//    for(int i=0;i<files.length;i++){
//    if(files[i].isDirectory())
//    folderList.add(files[i].getPath());
//    else{
//   fileList.add(files[i]);
//    InputStream photoStream = new FileInputStream(fileList.get(i));
//    sql = "INSERT INTO imgtable  (img) VALUES (?)";
//   
//    pstmt = conn.prepareStatement(sql);
//    pstmt.setBinaryStream(i, photoStream, (int) fileList.size());
//    pstmt.executeUpdate();
//    if(pstmt.executeUpdate()==1){
//    System.out.println("图片写入成功!");
//    }else{
//    System.out.println("图片写入失败!");  
//    }
//    }
//    }
////    for(Flie f:fileList){
////    f.getAbsoluteFile();
////  
////    }
//   }
   File files = new File("d:\\blog.jpg");
   InputStream photoStream = new FileInputStream(files);
   //sql = "   UPDATE   imgt   SET   img   =   ?   ";
   sql = "INSERT INTO imgtable  (img) VALUES (?)";
  
   pstmt = conn.prepareStatement(sql);
   pstmt.setBinaryStream(1, photoStream, (int) files.length());
   pstmt.executeUpdate();  
 
   if(pstmt.executeUpdate()==1){
   System.out.println("图片写入成功!");
   }else{
   System.out.println("图片写入失败!");  
   }
   stmt.close();
   pstmt.close();
   conn.close();
  } catch (Exception e) {
   e.printStackTrace();
  }
}
public static void main(String args[]){
  PutImg pi=new PutImg();
  pi.putimg();
}
}




//从数据库读取图片
package write_read;

import java.io.*;
import java.sql.*;
public class GetImg {

// private static final String URL="jdbc:db2://localhost:50000/SJPT";
// String username="DB_SJPT";
// String password="sjpt";
private static final String URL= "jdbc:mysql://localhost/img?useUnicode=true&characterEncoding=gb2312";
String username="root";
String password="root";
private Connection conn = null;
private PreparedStatement pstmt = null;
private ResultSet rs = null;
private File file = null;

public void blobRead(String outfile, int picID) throws Exception {
  FileOutputStream fos = null;
  InputStream is = null;
  byte[] Buffer = new byte[4096];
  try {
// Class.forName("com.ibm.db2.jcc.DB2Driver");
   Class.forName("com.mysql.jdbc.Driver");
   conn = DriverManager.getConnection(URL,username,password);
   System.out.println("测试连接成功");
   pstmt = conn.prepareStatement("select img from imgtable where id=?");
   pstmt.setInt(1, picID); // 传入要取的图片的ID
   rs = pstmt.executeQuery();
  
   System.out.println(rs);
   rs.next();
  
  
   file = new File(outfile);
   if (!file.exists()) {
    file.createNewFile(); // 如果文件不存在,则创建
   }
   fos = new FileOutputStream(file);
   is = rs.getBinaryStream("img");
   System.out.println("1000000"+is);
   int size = 0;
  
  while ((size = is.read(Buffer)) != -1) {
 
    // System.out.println(size);
    fos.write(Buffer, 0, size);
   }
  } catch (Exception e) {
   System.out.println( e.getMessage());
  } finally {
   // 关闭用到的资源
   fos.close();
   rs.close();
   pstmt.close();
   conn.close();
  }
}
public static void main(String[] args) {
  try {
   GetImg gi=new GetImg();
   gi.blobRead("E:/getimgs/liyanli.jpg", 5);
   System.out.println(gi);
   if(gi!=null){
   System.out.println("图片读取成功");
   }else{
   System.out.println("图片读取失败!");  
   }
  } catch (Exception e) {
   System.out.println("[Main func error: ]" + e.getMessage());
  }
}
}



分享到:
评论
1 楼 流星划过的天空 2012-07-10  
mysql建表脚本如下:
create database img;
use img;
create table imgtable(
id int primary key not null auto_increment,
img longblob);


注意:其中图片要以二进制形式存储到数据库,img 类型为
1. TINYBLOB 0-255字节 不超过 255 个字符的二进制字符串;
2. BLOB 0-65 535字节 二进制形式的长文本数据 ;
3. MEDIUMBLOB 0-16 777 215字节 二进制形式的中等长度文本数据 ;
4. LOGNGBLOB 0-4 294 967 295字节 二进制形式的极大文本数据。
具体类型视图片大小而定。

相关推荐

Global site tag (gtag.js) - Google Analytics