//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数据库中的图像的代码。当我从这个标记的另一个jsp文件调用这个时,我得到了一个错误 <img border="3" src="download_image.jsp" height="145" width="145" />
作为:
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中编写了如下代码&我也在web.xml中进行了Map,但我仍然没有看到图像:(
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条答案
按热度按时间sr4lhrrt1#
我不会显示来自jsp的图像。jsp被设计为服务于html页面,因此它将开始向输出流输出http报头,表示它是一个html页面。
然后在中间你说'哦不,我想发送一个图像',但到那时已经太晚了,这就是为什么你会得到这个错误
getOutputStream() has already been called for this response
.使用jsp输出图像没有任何好处,jsp的目的是使输出html页面变得容易。
改用普通的servlet。覆盖
doGet()
方法,并将输出图像的代码放入其中。不要忘记将servletMap添加到web.xml,以便为其提供路径。如果servletMap将Map到路径
/downloadImg
,你只是把<img border="3" src="downloadImg" height="145" width="145" />
相反,在另一个jsp中。1tu0hz3e2#
它们在这里:
您必须提到端口号:您应该在代码中更改为