当前位置: 技术问答>java相关
请教高手:如何读取数据库中的image对象,并在页面上显示出来?
来源: 互联网 发布时间:2015-02-14
本文导语: | 看下下面的示例 import javax.servlet.*; import javax.servlet.http.*; import java.sql.*; import java.io.*; public class photo extends HttpServlet { Connection conn=null; PreparedStatement pstmt=null; ResultSet rs=null; publ...
|
看下下面的示例
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import java.io.*;
public class photo extends HttpServlet
{
Connection conn=null;
PreparedStatement pstmt=null;
ResultSet rs=null;
public void init(ServletConfig conf) throws ServletException
{
super.init(conf);
try{
Class.forName("org.gjt.mm.mysql.Driver");
}catch(ClassNotFoundException e){}
String url="jdbc:mysql://localhost:3306/Sample?user=root;password=";
// String user="root",password="";
try{
conn=DriverManager.getConnection(url);
pstmt=conn.prepareStatement("select photo from person where id=?");
}catch(SQLException e){System.err.println("Something is error!");}
}
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException
{
int id=Integer.parseInt(request.getParameter("id"));
int length;
InputStream is=null;
byte[] buffer=new byte[4096];
response.setContentType("images/*");
DataOutputStream os=null;
try{
pstmt.setInt(1,id);
rs=pstmt.executeQuery();
os=new DataOutputStream(response.getOutputStream());
is=rs.getBinaryStream("photo");
}catch(SQLException e){}
while((length=is.read(buffer))!=-1)
{
os.write(buffer,0,length);
os.flush();
}
}
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException
{
doGet(request,response);
}
public void destroy()
{
try{
rs.close();
pstmt.close();
conn.close();
}catch(SQLException e){}
}
}
在JSP里的调用方式:
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import java.io.*;
public class photo extends HttpServlet
{
Connection conn=null;
PreparedStatement pstmt=null;
ResultSet rs=null;
public void init(ServletConfig conf) throws ServletException
{
super.init(conf);
try{
Class.forName("org.gjt.mm.mysql.Driver");
}catch(ClassNotFoundException e){}
String url="jdbc:mysql://localhost:3306/Sample?user=root;password=";
// String user="root",password="";
try{
conn=DriverManager.getConnection(url);
pstmt=conn.prepareStatement("select photo from person where id=?");
}catch(SQLException e){System.err.println("Something is error!");}
}
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException
{
int id=Integer.parseInt(request.getParameter("id"));
int length;
InputStream is=null;
byte[] buffer=new byte[4096];
response.setContentType("images/*");
DataOutputStream os=null;
try{
pstmt.setInt(1,id);
rs=pstmt.executeQuery();
os=new DataOutputStream(response.getOutputStream());
is=rs.getBinaryStream("photo");
}catch(SQLException e){}
while((length=is.read(buffer))!=-1)
{
os.write(buffer,0,length);
os.flush();
}
}
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException
{
doGet(request,response);
}
public void destroy()
{
try{
rs.close();
pstmt.close();
conn.close();
}catch(SQLException e){}
}
}
在JSP里的调用方式: