Chinaunix首页 | 论坛 | 博客
  • 博客访问: 422568
  • 博文数量: 77
  • 博客积分: 2303
  • 博客等级: 大尉
  • 技术积分: 808
  • 用 户 组: 普通用户
  • 注册时间: 2004-11-30 09:15
文章存档

2015年(1)

2013年(3)

2012年(2)

2011年(46)

2009年(4)

2008年(2)

2005年(12)

2004年(7)

我的朋友

分类: Java

2011-02-14 13:52:03

此实现为用java访问mysql的blob,对图片进行存取


  1. /**
  2.  * Title: BlobPros.java
  3.  * Project: test
  4.  * Description: 把图片存入mysql中的blob字段,并取出
  5.  * Call Module: mtools数据库中的tmp表
  6.  * File: C:downloadsluozsh.jpg
  7.  * Copyright: Copyright (c) 2003-2003
  8.  * Company: uniware
  9.  * Create Date: 2002.12.5
  10.  * @Author: ChenQH
  11.  * @version 1.0 版本*
  12.  *
  13.  * Revision history
  14.  * Name Date Description
  15.  * ---- ---- -----------
  16.  * Chenqh 2003.12.5 对图片进行存取
  17.  *
  18.  * note: 要把数据库中的Blob字段设为longblob
  19.  *
  20.  */

  21. //package com.uniware;

  22. import java.io.*;
  23. import java.util.*;
  24. import java.sql.*;

  25. public class BlobPros
  26. {
  27.     private static final String URL = "jdbc:mysql://10.144.123.63:3306/mtools?user=wind&password=123&useUnicode=true";
  28.     private Connection conn = null;
  29.     private PreparedStatement pstmt = null;
  30.     private ResultSet rs = null;
  31.     private File file = null;
  32.    
  33.     public BlobPros()
  34.     {
  35.     }

  36.     /**
  37.     * 向数据库中插入一个新的BLOB对象(图片)
  38.     * @param infile 要输入的数据文件
  39.     * @throws java.lang.Exception
  40.     */
  41.    public void blobInsert(String infile) throws Exception
  42.    {
  43.        FileInputStream fis = null;
  44.            try
  45.            {
  46.                Class.forName("org.gjt.mm.mysql.Driver").newInstance();
  47.                conn = DriverManager.getConnection(URL);
  48.            
  49.                file = new File(infile);
  50.                fis = new FileInputStream(file);
  51.                //InputStream fis = new FileInputStream(infile);
  52.          pstmt = conn.prepareStatement("insert into tmp(descs,pic) values(?,?)");
  53.          pstmt.setString(1,file.getName()); //把传过来的第一个参数设为文件名
  54.                //pstmt.setBinaryStream(2,fis,(int)file.length()); //这种方法原理上会丢数据,因为file.length()返回的是long型
  55.          pstmt.setBinaryStream(2,fis,fis.available()); //第二个参数为文件的内容
  56.          pstmt.executeUpdate();
  57.       }
  58.            catch(Exception ex)
  59.            {
  60.           System.out.println("[blobInsert error : ]" + ex.toString());
  61.            }
  62.         finally
  63.         {
  64.                //关闭所打开的对像//
  65.                pstmt.close();
  66.                fis.close();
  67.                conn.close();
  68.            }
  69.     }
  70.    

  71.     /**
  72.     * 从数据库中读出BLOB对象
  73.     * @param outfile 输出的数据文件
  74.     * @param picID 要取的图片在数据库中的ID
  75.     * @throws java.lang.Exception
  76.     */

  77.     public void blobRead(String outfile,int picID) throws Exception
  78.     {
  79.         FileOutputStream fos = null;
  80.         InputStream is = null;
  81.         byte[] Buffer = new byte[4096];
  82.  
  83.             try
  84.             {
  85.                 Class.forName("org.gjt.mm.mysql.Driver").newInstance();
  86.                 conn = DriverManager.getConnection(URL);
  87.                 pstmt = conn.prepareStatement("select pic from tmp where id=?");
  88.                 pstmt.setInt(1,picID); //传入要取的图片的ID
  89.                 rs = pstmt.executeQuery();
  90.                 rs.next();
  91.                      
  92.                 file = new File(outfile);
  93.                 if(!file.exists())
  94.                 {
  95.                     file.createNewFile(); //如果文件不存在,则创建
  96.                 }
  97.                 fos = new FileOutputStream(file);
  98.                 is = rs.getBinaryStream("pic");
  99.                 int size = 0;
  100.                /* while(size != -1)
  101.                 {
  102.                     size = is.read(Buffer); //从数据库中一段一段的读出数据
  103.                     //System.out.println(size);
  104.                     if(size != -1) //-1表示读到了文件末
  105.                         fos.write(Buffer,0,size);
  106.                 } */
  107.                 while((size = is.read(Buffer)) != -1)
  108.     {
  109.                     //System.out.println(size);
  110.                     fos.write(Buffer,0,size);
  111.     }
  112.                               
  113.          }
  114.             catch(Exception e)
  115.             {
  116.                 System.out.println("[OutPutFile error : ]" + e.getMessage());
  117.             }
  118.             finally
  119.             {
  120.                 //关闭用到的资源
  121.                 fos.close();
  122.                 rs.close();
  123.                 pstmt.close();
  124.                 conn.close();
  125.             }
  126.     }
  127.     
  128.     public static void main(String[] args)
  129.     {
  130.         try
  131.         {
  132.            
  133.             BlobPros blob = new BlobPros();
  134.             //blob.blobInsert("C:Downloadsluozsh1.jpg");
  135.             blob.blobRead("c:/downloads/1.jpg",47);
  136.         }
  137.         catch(Exception e)
  138.         {
  139.             System.out.println("[Main func error: ]" + e.getMessage());
  140.         }
  141.     }
  142. }
阅读(4157) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~