说明:sql2000数据库的
图片字段是<Binary>的
1,存入代码
<%@ page contentType="text/html;charset=gb2312" %>
<%@page import="java.sql.*"%>
<%@page import="java.util.*"%>
<%@page import="java.io.*"%>
<%@page import="java.text.*"%>
<%
String content=request.getParameter("content");
String filename=request.getParameter("image");
FileInputStream str=new FileInputStream(filename);
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").getInterfaces();
Connection conn=java.sql.DriverManager.getConnection
("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=gamestar", "sa", "");String
sql="insert into image(name,img) values(?,?)";
PreparedStatement pstmt=conn.prepareStatement(sql);
pstmt.setString(1,content);
pstmt.setBinaryStream(2,str,str.available());
pstmt.execute();
%>
2,读出代码
<%@ page contentType="image/gif;charset=gb2312" %>
<%@ page import="java.sql.*" %>
<%@ page import="java.util.*"%>
<%@ page import="java.text.*"%>
<%@ page import="java.io.*"%>
<%
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").getInterfaces();
Connection conn=java.sql.DriverManager.getConnection
("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=gamestar", "sa", "");
Statement stmt=conn.createStatement();
int id=45;
//int id= Integer.parseInt(request.getParameter("id"));
String sql = "select * from image WHERE name="+id+"";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next())
{
java.io.InputStream in=rs.getBinaryStream("img");
java.io.OutputStream myout=response.getOutputStream();
int len=0;
byte[] buffer=new byte[1024];
while((len=in.read(buffer))>0)
{
myout.write(buffer,0,len);
}
in.close();
myout.flush();
myout.close();
}
%>
3,显示结果
是一张显示不出来的图片,是显示不出来的图片哦