【问题标题】:error getting while retrieving image from mysql database从 mysql 数据库中检索图像时出错
【发布时间】:2013-04-04 12:16:05
【问题描述】:

我想从数据库中检索图像并将其显示到 jsp 页面。所以我创建了一个jsp页面,但我尝试了很多错误但无法修复它。 我可以在 servlet 中轻松做到这一点,但我需要在 jsp 中

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

<%// declare a connection by using Connection interface Connection connection = null;
/* Create string of connection url within specified format with machine 
name, port number and database name. Here machine name id localhost 
and database name is mahendra. */
Blob image = null;
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
DataBaseConnection db= new DataBaseConnection();
ServletOutputStream out1 = response.getOutputStream();
try {
Class.forName("com.mysql.jdbc.Driver");
con=db.connet();
stmt = con.createStatement();
rs = stmt.executeQuery("select img from one where  id = '4'");
if (rs.next()) {
image = rs.getBlob(1);
} else {
response.setContentType("text/html");

out.println("<font color='red'>image not found for given id</font>");

return;
}
response.setContentType("image/gif");
InputStream in = image.getBinaryStream();
int length = (int) image.length();
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
while ((length = in.read(buffer)) != -1) {
out1.write(buffer, 0, length);
}
in.close();
out.flush();

} catch (Exception e) {
response.setContentType("text/html");
out.println("<html><head><title>Unable To Display image</title></head>");
out.println("<body><h4><font color='red'>Image Display Error=" + e.getMessage() +
"</font></h4></body></html>");
return;
} 


%>

错误是

HTTP Status 500 - java.lang.IllegalStateException: getOutputStream() has already been called for this response

type Exception report

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

description The server encountered an internal error that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: java.lang.IllegalStateException: getOutputStream() has already been called for this response
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:585)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:465)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
root cause

java.lang.IllegalStateException: getOutputStream() has already been called for this response
    org.apache.catalina.connector.Response.getWriter(Response.java:639)
    org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:214)
    org.apache.jasper.runtime.JspWriterImpl.initOut(JspWriterImpl.java:125)
    org.apache.jasper.runtime.JspWriterImpl.flushBuffer(JspWriterImpl.java:118)
    org.apache.jasper.runtime.PageContextImpl.release(PageContextImpl.java:190)
    org.apache.jasper.runtime.JspFactoryImpl.internalReleasePageContext(JspFactoryImpl.java:126)
    org.apache.jasper.runtime.JspFactoryImpl.releasePageContext(JspFactoryImpl.java:80)
    org.apache.jsp.retrieveImage_jsp._jspService(retrieveImage_jsp.java:123)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.37 logs.

Apache Tomcat/7.0.37

【问题讨论】:

    标签: java mysql image jsp servlets


    【解决方案1】:

    这是因为你的 JSP 中有这段代码。

    ServletOutputStream out1 = response.getOutputStream();
    

    一个好的经验法则是不要在 JSP 中使用 ServletOutputStream

    附带说明:使用 Servlet 执行此类任务并使用 JSP 进行演示 其次,在写响应之前设置response.setContentType();

    【讨论】:

    • 那我该怎么办?我应该在哪里改变
    • 我必须在 jsp 页面上显示图像,所以这是我认为的唯一方法。如果你建议我任何其他方式,那么欢迎你
    • 使用Servlet发送图片。
    • 请参阅我必须显示带有其他详细信息的图像。我已经做了一个 jsp 页面来获取其他详细信息。现在我也被要求显示图像。所以我问怎么做
    • 给我一些建议来做同样的事情
    【解决方案2】:

    在 servlet 中,您可以创建输出流对象。但是在 jsp 中,不能创建 outputstream 对象。取而代之的是,您必须使用 implicit object out。这就是为什么它抛出错误,因为 getOutputStream() 方法已经被调用。所以删除该行,并使用隐式对象。

    【讨论】:

    • 我已经检查过了,但它不起作用,因为我有三个参数
    猜你喜欢
    • 2021-10-07
    • 1970-01-01
    • 2015-06-09
    • 2014-02-19
    • 2015-07-06
    • 2013-01-08
    • 1970-01-01
    • 1970-01-01
    • 2013-03-18
    相关资源
    最近更新 更多