我用servlet读取数据库图片文件已经成功

来源:互联网 发布:用数据库统计访问次数 编辑:程序博客网 时间:2024/05/04 08:05

我用servlet读取数据库图片文件已经成功
可以单独显示
现在要在jsp页面上显示出来,同时显示其他详细信息
需要如何设置
使用 <img>还是其他的

servlet主要代码

Java code
<!--Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->import java.io.*;import java.util.*;import javax.servlet.*;import javax.servlet.http.*;import java.sql.*;public class imageShow extends HttpServlet { private ServletConfig config; //初始化Servlet final public void init(ServletConfig config) throws ServletException { this.config = config; } //处理GET请求 public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException { doPost(request,response); } //响应POST请求 protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=GBK"); ServletOutputStream sout=response.getOutputStream(); try { Class.forName("com.mysql.jdbc.Driver"); }catch(ClassNotFoundException ce) { sout.println(ce.getMessage()); } try { //建立数据库连接 String url="jdbc:mysql://localhost:3306/test"; String user="root"; String pass=""; Connection conn=DriverManager.getConnection(url,user,pass); String temp=request.getParameter("id"); int id=Integer.parseInt(temp); String sql = "select picture from book where id ="+id; Statement stmt=conn.createStatement(); ResultSet rs=stmt.executeQuery(sql); InputStream in=null; if(rs.next()) { in=rs.getBinaryStream("picture"); //ServletOutputStream sout=response.getOutputStream(); byte image[]=new byte[1024]; while(in.read(image)!=-1) { sout.write(image); } sout.flush(); //sout.close(); rs.close(); stmt.close(); conn.close(); //response.sendRedirect("pleaselogin.jsp?errorMessage=1"); return; //request.getRequestDispatcher("/site2/index.jsp").forward(request,response); } } catch (Exception e){ sout.println("图片不能显示.<br>"); sout.println("Error : " + e.toString()); } }}
原创粉丝点击