gpt4 book ai didi

java - 在检索图像时已为此响应调用 getOutputStream()

转载 作者:行者123 更新时间:2023-11-29 12:04:00 25 4
gpt4 key购买 nike

r_image.jsp

    <jsp:include page="precheck.jsp"></jsp:include>
<%@page import="java.io.InputStream"%>
<%@page import="java.io.FileInputStream"%>
<%@page import="java.io.File"%>
<%@page import="java.io.*"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.Connection"%>
<%@page language="java" trimDirectiveWhitespaces="true"%>
<%@page import="java.sql.*"%>
<%@ 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">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

<%
Connection conn;
PreparedStatement pstm;
ResultSet rs;
String url = "jdbc:mysql://localhost:3306/inventory";//Here the "test" is the Database name
try{
String sid=(String)session.getAttribute("session-sid");
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url, "root", "");
String q = "select * from tblstore where sid='"+sid+"'";
PreparedStatement ps = conn.prepareStatement(q);
ResultSet rs1 = ps.executeQuery();
int i = 0;

if(rs1.next()){
String imgLen = rs1.getString(1);
int len = imgLen.length();
System.out.println("Length:"+len);
byte [] rb = new byte[len];
InputStream readImg = rs1.getBinaryStream(1);
int index=readImg.read(rb, 0, len);
response.setContentType("image/jpg");
OutputStream obj=response.getOutputStream();
obj.write(rb,0,len);
obj.flush();
obj.close();
return;
}

}
catch(Exception ex){
System.out.println(ex.getMessage());
}
%>

</body>
</html>

profile.html

<img src="r_image.jsp" />

我希望使用上面的 HTML 标签检索所需的图像,它的工作方式就像一个魅力,但在后台播放异常,说明原因:异常

    SEVERE: Servlet.service() for servlet [jsp] in context with path [/inv] 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:627)
at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:215)
at org.apache.jasper.runtime.JspWriterImpl.initOut(JspWriterImpl.java:125)
at org.apache.jasper.runtime.JspWriterImpl.flushBuffer(JspWriterImpl.java:118)
at org.apache.jasper.runtime.PageContextImpl.release(PageContextImpl.java:190)
at org.apache.jasper.runtime.JspFactoryImpl.internalReleasePageContext(JspFactoryImpl.java:123)
at org.apache.jasper.runtime.JspFactoryImpl.releasePageContext(JspFactoryImpl.java:80)
at org.apache.jsp.r_005fimage_jsp._jspService(r_005fimage_jsp.java:124)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:433)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:389)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:333)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:185)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:151)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:405)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:269)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:515)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:722)

为什么在图像仍在检索并且效果很好时出现此异常!

最佳答案

您应该使用 Servlet 在响应中发送二进制数据,而不是 JSP 页面。 JSP 页面发送文本输出,并将调用 getWriter 方法以从 JSP 页面发送缓冲的输出。

另请参阅java.lang.IllegalStateException: getOutputStream() has already been called for this response

关于java - 在检索图像时已为此响应调用 getOutputStream(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31870364/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com