我想显示/检索数据库中的所有图像到jsp页面

时间:2015-08-20 14:12:49

标签: java jsp servlets

我是第一次做这个任务所以我谷歌这个代码,并尝试了解其中的内容。我喜欢实现这个代码之后的是它无法满足我的要求,它只显示一个图像,它从数据库中撤回了所有图像以及抛出异常 -

java.lang.IllegalStateException:已经为此响应调用了getOutputStream()。

在我谷歌这个例外之后,我在这个特定网站上读到的关于这个问题就在这个问题中 -

java.lang.IllegalStateException: getOutputStream() has already been called for this response

我再次尝试这样做,但没有得到结果。无论我在谷歌这个例外之前做过什么,之后我会告诉你

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<a href="index.jsp">click to display images</a>
</body>
</html>

这是我的index.jsp页面

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"     "http://www.w3.org/TR/html4/loose.dtd">

<%@ page import="java.io.*"%>

<%@ page import="java.sql.*,java.util.*"%>
<%!ServletOutputStream o; %>
<%!ArrayList<Blob> bl; %>


<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>men's trousers</title>
</head>
<body>


<%
try {

Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con =DriverManager.getConnection("jdbc:mysql://localhost:3306      /imgdb","root","annie1993");
PreparedStatement stmt = con.prepareStatement("select IMAGE from myimages ");
ResultSet rs = stmt.executeQuery();
//ResultSet rs1=stmt.executeQuery("select count(id) from student_profile");
bl=new ArrayList<Blob>();
while(rs.next()) {
System.out.println("st-1");
bl.add(rs.getBlob(1));
System.out.println("st-2");
}
//if(rs1.next())
//System.out.println(bl.size()+"size="+rs1.getInt(1));
for(int i=0;i<bl.size();i++){
System.out.println("st-3");
Blob b=bl.get(i);
System.out.println("st-4 "+b);
byte brr[]=new byte[(int)b.length()];
System.out.println("st-6");
brr=b.getBytes(1,(int)b.length());
System.out.println("st-7 "+brr);
// display the image
response.setContentType("image/jpg");
o = response.getOutputStream();
System.out.println("st-8 "+o);
%><table><tr><td align='left'>
<img src=<%o.write(brr); %> width="117" height="160"
>
</td></tr></table>
<%
out.println("<br>");
System.out.println("st-9 ");
}
} catch (Exception e) {
out.println("Unable To Display image");
out.println("Image Display Error=" + e.getMessage());
return;
} 
%>
</div></td></tr></table></td></tr>
</table>


</body>
</html>

我得到的结果是

INFO: Server startup in 6916 ms
st-1
st-2
st-1
st-2
st-1
st-2
st-1
st-2
st-1
st-2
st-3
st-4 com.mysql.jdbc.Blob@1a368ee
st-6
st-7 [B@886ce4
st-8 org.apache.catalina.connector.CoyoteOutputStream@cea65d
st-9 
st-3
st-4 com.mysql.jdbc.Blob@1259b5d
st-6
st-7 [B@1c9704f
st-8 org.apache.catalina.connector.CoyoteOutputStream@cea65d
st-9 
st-3
st-4 com.mysql.jdbc.Blob@7fc51e
st-6
st-7 [B@1d06ac2
st-8 org.apache.catalina.connector.CoyoteOutputStream@cea65d
st-9 
st-3
st-4 com.mysql.jdbc.Blob@bcf156
st-6
st-7 [B@bb7c6
st-8 org.apache.catalina.connector.CoyoteOutputStream@cea65d
st-9 
st-3
st-4 com.mysql.jdbc.Blob@4ccfac
st-6
st-7 [B@f42b5d
st-8 org.apache.catalina.connector.CoyoteOutputStream@cea65d
st-9 
Aug 20, 2015 6:28:43 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [jsp] in context with path   [/display_multiple_images] threw exception [java.lang.IllegalStateException: getOutputStream() has already been called for this response] with root cause
java.lang.IllegalStateException: getOutputStream() has already been called for this response
at org.apache.catalina.connector.Response.getWriter(Response.java:578)
at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:212)
at org.apache.jasper.runtime.JspWriterImpl.initOut(JspWriterImpl.java:115)
at org.apache.jasper.runtime.JspWriterImpl.flushBuffer(JspWriterImpl.java:108)
at org.apache.jasper.runtime.PageContextImpl.release(PageContextImpl.java:183)
at org.apache.jasper.runtime.JspFactoryImpl.internalReleasePageContext(JspFactoryImpl.java:120)
at org.apache.jasper.runtime.JspFactoryImpl.releasePageContext(JspFactoryImpl.java:75)
at org.apache.jsp.index_jsp._jspService(index_jsp.java:184)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
at   org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:43    1)
at  org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:516)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1086)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:659)
at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:223)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1558)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1515)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at  java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)

Aug 20, 2015 6:28:44 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [jsp] in context with path  [/display_multiple_images] threw exception [java.lang.IllegalStateException: getOutputStream() has already been called for this response] with root cause
java.lang.IllegalStateException: getOutputStream() has already been called for this response
at org.apache.catalina.connector.Response.getWriter(Response.java:578)
at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:212)
at org.apache.jasper.runtime.JspWriterImpl.initOut(JspWriterImpl.java:115)
at org.apache.jasper.runtime.JspWriterImpl.flushBuffer(JspWriterImpl.java:108)
at org.apache.jasper.runtime.PageContextImpl.release(PageContextImpl.java:183)
at org.apache.jasper.runtime.JspFactoryImpl.internalReleasePageContext(JspFactoryImpl.java:120)
at org.apache.jasper.runtime.JspFactoryImpl.releasePageContext(JspFactoryImpl.java:75)
at org.apache.jsp.index_jsp._jspService(index_jsp.java:184)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:431)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:516)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1086)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:659)
at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:223)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1558)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1515)
at   java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)

st-1
st-2
st-1
st-2
st-1
st-2
st-1
st-2
st-1
st-2
st-3
st-4 com.mysql.jdbc.Blob@1280329
st-6
st-7 [B@1dc1aa
st-8 org.apache.catalina.connector.CoyoteOutputStream@cea65d
st-9 
st-3
st-4 com.mysql.jdbc.Blob@5de911
st-6
st-7 [B@47eb95
st-8 org.apache.catalina.connector.CoyoteOutputStream@cea65d
st-9 
st-3
st-4 com.mysql.jdbc.Blob@197937f
st-6
st-7 [B@1190cf6
st-8 org.apache.catalina.connector.CoyoteOutputStream@cea65d
st-9 
st-3
st-4 com.mysql.jdbc.Blob@1375e5
st-6
st-7 [B@12323b1
st-8 org.apache.catalina.connector.CoyoteOutputStream@cea65d
st-9 
st-3
st-4 com.mysql.jdbc.Blob@664f41
st-6
st-7 [B@1bb8fc7
st-8 org.apache.catalina.connector.CoyoteOutputStream@cea65d
st-9 

之后我谷歌这个例外,我做了什么,我无法得到一个单一的图像,但这次它没有抛出任何异常 这是代码

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<a href="Displayimage">click to display images</a>
</body>
</html>

这是我的Displayimage servlet

package com.mulimgdis;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* Servlet implementation class Displayimage
*/
public class Displayimage extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
 * @see HttpServlet#HttpServlet()
 */
 public Displayimage() {
    super();
    // TODO Auto-generated constructor stub
}

/**
 * @see HttpServlet#doGet(HttpServletRequest request,  HttpServletResponse response)
 */
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub


    PrintWriter out = response.getWriter();

    ServletOutputStream o; 
    ArrayList<Blob> bl;
    try {




         Class.forName("com.mysql.jdbc.Driver").newInstance();
    Connection con  =DriverManager.getConnection("jdbc:mysql://localhost:3306/imgdb","root","annie1993");
    PreparedStatement stmt = con.prepareStatement("select IMAGE from myimages ");
    ResultSet rs = stmt.executeQuery();
    //ResultSet rs1=stmt.executeQuery("select count(id) from student_profile");
     bl=new ArrayList<Blob>();
    while(rs.next()) {
    System.out.println("st-1");
    bl.add(rs.getBlob(1));
    System.out.println("st-2");
    }
    //if(rs1.next())
    //System.out.println(bl.size()+"size="+rs1.getInt(1));
    for(int i=0;i<bl.size();i++){
    System.out.println("st-3");
    Blob b=bl.get(i);
    System.out.println("st-4 "+b);
    byte brr[]=new byte[(int)b.length()];
    System.out.println("st-6");
    brr=b.getBytes(1,(int)b.length());
    System.out.println("st-7 "+brr);
    // display the image
    response.reset();
    response.setContentType("image/jpg");
     o = response.getOutputStream();
     System.out.println("st-8 "+o);

     /* 
      * 
      *   %><table><tr><td align='left'>
 <img src=<%o.write(brr); %> width="117" height="160"
 >
 </td></tr></table>
 <%
 out.println("<br>");
 System.out.println("st-9 ");
 }
 } catch (Exception e) {
out.println("Unable To Display image");
out.println("Image Display Error=" + e.getMessage());
return;
} 
%>
</div></td></tr></table></td></tr>
</table>
*/

     o.write(brr);
     response.getOutputStream().flush();  

     out.println("<br>");
     System.out.println("st-9 ");
     }
    } catch (Exception e) {
    out.println("Unable To Display image");
    out.println("Image Display Error=" + e.getMessage());
    return;
    }  


}

1 个答案:

答案 0 :(得分:0)

查看此文档:httpServletResponse.getOutputStream()

你看到了

  

可以调用此方法或getWriter()来编写正文,而不是两者。

应该是问题