如何在数据库中显示图像到JSP页面

时间:2014-03-11 10:21:25

标签: java html mysql jsp servlets

//download_image.jsp file
<%@page import="java.io.InputStream"%>
<%@page import="java.io.OutputStream"%>
<%@page import="java.sql.Blob"%>
<%@page import="java.sql.ResultSet"%>
<%
             //   String username = session.getAttribute("userid").toString();

           try {

                OutputStream oImage;
                DB.Connect.openConnection();
                String query = "select image from tbluser where userid = '1'";
                ResultSet rs = DB.Connect.stat.executeQuery(query);
                if (rs.next()) {
                    byte barray[] = rs.getBytes(1);
                    response.reset();                   
                    response.setContentType("image/jpeg");
                    oImage = response.getOutputStream();
                    oImage.write(barray);
                    oImage.flush();
                    oImage.close();
                }
               DB.Connect.CloseConnection();
            } catch (Exception e) {
                e.printStackTrace();
            }
%
  

块引用

这是我从mysql db显示图像的代码。 当我从这个标签的另一个jsp文件中调用它时,我收到错误

  

<img border="3" src="download_image.jsp" height="145" width="145" />

as:

WARNING: StandardWrapperValve[jsp]: PWC1406: Servlet.service() for servlet jsp threw exception
java.lang.IllegalStateException: PWC3991: getOutputStream() has already been called for this response
        at org.apache.catalina.connector.Response.getWriter(Response.java:707)
        at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:224)
        at org.apache.jasper.runtime.JspWriterImpl.initOut(JspWriterImpl.java:187)
        at org.apache.jasper.runtime.JspWriterImpl.flushBuffer(JspWriterImpl.java:180)
        at org.apache.jasper.runtime.PageContextImpl.release(PageContextImpl.java:237)
        at org.apache.jasper.runtime.JspFactoryImpl.internalReleasePageContext(JspFactoryImpl.java:181)
        at org.apache.jasper.runtime.JspFactoryImpl.releasePageContext(JspFactoryImpl.java:132)
        at org.apache.jsp.download_005fimage_jsp._jspService(download_005fimage_jsp.java from :99)
        at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:109)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
        at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:406)
        at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:483)
        at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:373)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
        at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1523)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:279)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:188)
        at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:641)
        at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:97)
        at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:85)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:185)
        at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:325)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:226)
        at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:165)
        at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:791)
        at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:693)
        at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:954)
        at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:170)
        at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:135)
        at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:102)
        at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:88)
        at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:76)
        at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:53)
        at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:57)
        at com.sun.grizzly.ContextTask.run(ContextTask.java:69)
        at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:330)
        at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:309)
        at java.lang.Thread.run(Thread.java:619)

我该怎么办请向我推荐一种方法。


我已将我的代码编写为 下面的displayImage servlet&amp;我也在web.xml中进行了映射,但我仍然在图像上显示空白:(

public class displayImage extends  HttpServlet{
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        //PrintWriter pw = response.getWriter();
        String connectionURL = "jdbc:mysql://localhost/medico";
        java.sql.Connection con=null;
        try{
        Class.forName("com.mysql.jdbc.Driver").newInstance();
            con=DriverManager.getConnection(connectionURL,"root","");
            Statement st1=con.createStatement();
            ResultSet rs1 = st1.executeQuery("select image from tbluser where userid='1'");
            String imgLen="";
            if(rs1.next()){
                imgLen = rs1.getString(1);

            }
            rs1 = st1.executeQuery("select image from tbluser where userid='1'");
            if(rs1.next()){
                int len = imgLen.length();
                byte [] rb = new byte[len];
                InputStream readImg = rs1.getBinaryStream(1);
                int index=readImg.read(rb, 0, len);             
                st1.close();
                response.reset();
                response.setContentType("image/jpg");               response.getOutputStream().write(rb,0,len);
                response.getOutputStream().flush();
            }
        }
        catch (Exception e){
            e.printStackTrace();
        }
    }

}

我在html文件中将其称为。 所以错误在哪里:(这里

2 个答案:

答案 0 :(得分:2)

我不会在JSP中显示图像。 JSP旨在为HTML页面提供服务,因此它将开始输出HTTP标头,表明它是输出流的HTML页面。

然后在它的中间你说'哦不,我想发送一个图像',但到那时已经太晚了,这就是你得到错误的原因getOutputStream() has already been called for this response

使用JSP输出图像没有任何好处,JSP的目的是使输出HTML页面变得容易。

请改用普通的servlet。覆盖doGet()方法,只需将输出图像的代码放入其中。不要忘记将servlet映射添加到web.xml,以便为其指定路径。

因此,如果您的servlet映射将映射到路径/downloadImg,那么您只需将<img border="3" src="downloadImg" height="145" width="145" />放在另一个JSP中。

答案 1 :(得分:1)

他们的错误就在这里:

String connectionURL = "jdbc:mysql://localhost/medico";

您必须提及端口号:您应该将代码更改为

String connectionURL = "jdbc:mysql://localhost:3306/medico";