[学习笔记]二进制数据的处理(1) 普通二进制数据处理 B -------读取数据库中二进制数据

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="cn.java.*"%>
<%@page import="java.sql.*"%>
<%@page import="java.text.*"%>
<%@page import="java.io.*"%>
<%@page import="java.util.*"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   
"http://www.w3.org/TR/html4/loose.dtd">

<html>
    
<head>
        
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        
<title>JSP Page</title>
    
</head>
    
<body>
  
<%

  
             
                Connection conn 
= null;
               Statement stmt
=null;
                ResultSet rs
=null;
            
try ...{
              
                String sql 
= "select binfile from bindata where id=5";
                conn 
= DBUtil.getConnection();
               stmt
=conn.createStatement();
         
                rs
=stmt.executeQuery(sql);
             
while(rs.next())...{
                 ServletOutputStream sout
=response.getOutputStream();
                 InputStream 
in=rs.getBinaryStream(1);
                 
byte b[] = new byte[0x7a120];//获得二进制输入流
                 while(in.read(b)!=-1)...
                 sout.write(b);
//输出字符流
                 }

                 
                 sout.flush();
                 sout.close();
//关闭sout输出流
             }

       
        
             
            }
  catch (SQLException ex) ...{
                out.println(ex.getMessage());
            }


            
finally ...{
                
try ...{
                 
                    DBUtil.close(rs, stmt, conn);
                }
 catch (Exception ex) ...{
               out.println(ex.getMessage());
                }

            }

%>
    
</body>
</html>
阅读更多
文章标签: 数据库 sql
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭