`
啸笑天
  • 浏览: 3438938 次
  • 性别: Icon_minigender_1
  • 来自: China
社区版块
存档分类
最新评论

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

    博客分类:
  • jsp
阅读更多

我用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()); } } }
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics