把图片a存储到sql server里面,在存入之前调用InputStream的available()是234521的数
可以用ResultSet从数据库里面读出图片a的时候再调用InputStream的available()怎么返回的就是0了
这是怎么回事?

解决方案 »

  1.   

    public void insert()throws Exception{
    String path="C:/test-3.gif";
    File file=new File(path);
    InputStream is = new FileInputStream(file);
    Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
    Connection con=DriverManager.getConnection("jdbc:microsoft:sqlserver://192.168.202.107:1433;DatabaseName=jk","jkuser","jkuser");
    PreparedStatement ps=con.prepareStatement("INSERT INTO big_object VALUES (?, ?)");
    ps.setString(1, file.getName());
    System.out.println(is.available());//有值234521
    ps.setBinaryStream(2, is, is.available());
    ps.executeUpdate();
    ps.close();
    is.close();
    con.close();
    }
    public void select() throws Exception{
    Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
    Connection con=DriverManager.getConnection("jdbc:microsoft:sqlserver://192.168.202.107:1433;DatabaseName=jk","jkuser","jkuser");
    ResultSet rs=con.createStatement().executeQuery("select text from big_object where name='test-3.gif'");
    rs.next();
                      InputStream is=rs.getBinaryStream(1);
                      System.out.println(is.available());//打出来的是0 为什么会这样?   
    byte[] buffer=new byte[1024];
    is.read(buffer);
    FileOutputStream fout=new FileOutputStream("c:/a_copy.gif");
        fout.write(buffer);
        fout.close();
        is.close();
    con.close();
    }
      

  2.   

    参看jdk中的InputStream的available方法,他永远返回available,所以你是无法读出的,以下是我的实现,由于没有下载Microsoft Jdbc For SQL Server,所以用jdbc-odbc实现的,这个无所谓的,如果图片很大,可以考虑采用BufferedOutputStream来进行文件输出缓冲。import java.io.*;
    import java.sql.*;public class TestWriteImage
    {
    static void WriteImage() throws Exception
    {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    String url = ("jdbc:odbc:Demo");
    Connection conn = DriverManager.getConnection(url);

    FileInputStream fis =  new FileInputStream("C:\\1.jpg");
    PreparedStatement ps = conn.prepareStatement("insert into MyTable values(?,?,?)");
    ps.setInt(1,1);
    ps.setString(2,"1,jpg");
    ps.setBinaryStream(3,fis,fis.available());

    ps.executeUpdate();

    fis.close();
    ps.close();
    conn.close();
    }

    static void ReadImage() throws Exception
    {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    String url = ("jdbc:odbc:Demo");
    Connection conn = DriverManager.getConnection(url);

    Statement s = conn.createStatement();
    String sql = "select img from MyTable where id = 1";

    //String sql = "select lastname from employees where employeeid = 1";

    ResultSet rs = s.executeQuery(sql);

    rs.next();

    InputStream is = rs.getBinaryStream(1);

    BufferedInputStream bis = new BufferedInputStream(is);
    File f = new File("C:\\2.jpg");
    if(f.exists()==false)
    {
    f.createNewFile();
    }
    FileOutputStream fos = new FileOutputStream(f);

    int temp;
    temp = bis.read();

    while(temp!=-1)
    {
    fos.write(temp);
    temp = bis.read();
    }
    fos.flush();
    fos.close();
    rs.close();
    s.close();
    conn.close();
    }

    public static void main(String[] args) throws Exception
    {
    WriteImage();
    ReadImage();
    }
    }