ydsakyclguozi 发表于 2013-2-6 08:06:12

JSP 读取 SQL Server 2000 中 image 型数据生成图片文件

<%@ page language="java" import="java.sql.*,java.io.*"
pageEncoding="GBK"%>
<%
    //建立连接对象
    Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver")
            .newInstance();
    String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=test", user = "sa", password = "sa";
    Connection conn = DriverManager.getConnection(url, user, password);
    Statement stmt = conn.createStatement();
    ResultSet rs;
    String strSql = "select id, data FROM test";
    InputStream in = null;
    FileOutputStream fileOutStream = null;
    rs = stmt.executeQuery(strSql);
    while (rs.next())
    {
      //Windows 系统下存储路径用2个反斜杠
      DataOutputStream sos = new DataOutputStream(
                new BufferedOutputStream(new FileOutputStream("c:\\"
                        + rs.getString("id") + ".jpg")));
      //读出流用getBinaryStream()方法。
      in = rs.getBinaryStream("data");
      //用缓存数组逐渐输出流
      int len = 0;
      byte[] b = new byte;
      while ((len = in.read(b)) != -1)
      {
            sos.write(b, 0, len);
      }
      sos.close();
      in.close();
    }
    rs.close();
    conn.close();
%>
页: [1]
查看完整版本: JSP 读取 SQL Server 2000 中 image 型数据生成图片文件