<%@   page   contentType="text/html;charset=gb2312"%>   
<%@   page   import="java.sql.*"   %>   
<%@   page   import="java.util.*"%>   
<%@   page   import="java.text.*"%>   
<%@   page   import="java.io.*"%>  
<%@ page import="com.microsoft.jdbc.sqlserver.SQLServerDriver"%>  
<html>   
<body>   
<%
 String classforname="com.microsoft.jdbc.sqlserver.SQLServerDriver";
String url="jdbc:microsoft:sqlserver://192.168.0.31:1433;DatabaseName=ylxt";
String user="sa";
String password="111111";
Class.forName(classforname);
Connection con=java.sql.DriverManager.getConnection(url,user,password); 
String   sql   =   "select   contents   from   wenxian   where   id=13";   
Statement   stmt=null;   
ResultSet   rs=null;   
try{   
stmt=con.createStatement();   
rs=stmt.executeQuery(sql);   
}catch(SQLException   e){}   
try   {   
while(rs.next())   {   
response.setContentType("image/jpeg");   
ServletOutputStream   sout   =   response.getOutputStream();   
InputStream   in   =   rs.getBinaryStream(1);   
byte   b[]   =   new   byte[0x7a120];   
for(int   i   =   in.read(b);   i   !=   -1;)   
{   
sout.write(b);   
in.read(b);   
}   
sout.flush();   
sout.close();   
}   
}   
catch(Exception   e){System.out.println(e);}   
%>   
</body>   
</html>   问题:我要从数据库中读取此文件的名字及二进制字段,然后写成一个文件放到客户端,就是下载吧
String   sql   =   "select name,contents from  wenxian where id=13"; 
急求高手!!
上面是我从数据库读取一个二进制图片并显示的方法,请问怎么样改!!

解决方案 »

  1.   

    response.setContentType("APPLICATION/OCTET-STREAM");  
     String filename="a.gif"; 
     response.setHeader("Content-Disposition","attachment;   filename=   \""   +   filename   +   "\"");    
    //ServletOutputStream   sout   =   response.getOutputStream();   
    InputStream   in   =   rs.getBinaryStream(1);  int   i;   
            while   ((i=in.read())   !=   -1)   {   
            out.write(i);   
            }   
    }
    }   
    catch(Exception   e){System.out.println(e);}   
    %>   
    为什么保存下来的文件不能显示啊,下载下来的文件损坏了