在JSP页面中,我想显示存储在数据库中的图像。我该怎么做?
我尝试了以下JSP代码,但它在浏览器中没有显示任何内容。我做错了什么?
<%
HttpSession ses2=request.getSession();
String email2 = (String)ses2.getAttribute("ses_email");
Connection cn2;
PreparedStatement ps2;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
cn2=DriverManager.getConnection("jdbc:odbc:sm","system","gecbsp");
ps2=cn2.prepareStatement("select * from smreg3 where email = "+
"'"+email2+"'");
ResultSet rs2=ps2.executeQuery();
//ServletOutputStream sos=response.getOutputStream();
if(rs2.next())
{
byte[] bytearray = new byte[1024];
int size=0;
InputStream image;
image = rs2.getBinaryStream(1);
response.reset();
response.setContentType("image/jpeg");
while((size=image.read(bytearray))!= -1 )
{
response.getOutputStream().write(bytearray,0,size);
}
response.flushBuffer();
image.close();
rs2.close();
}
}
catch(Exception ee)
{
ee.printStackTrace();
}
%>
错误:
java.lang.IllegalStateException
at org.apache.catalina.connector.ResponseFacade.reset(ResponseFacade.java:310)
at org.apache.jsp.profile_jsp._jspService(profile_jsp.java:193)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:393)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:263)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:584)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Thread.java:619)
答案 0 :(得分:0)
只需将此代码移动到Servlet,您应该好好去。在您的情况下,响应不能直接使用,因为JSP可能已经将头信息写入响应中。
在Servlet中,您的代码如下所示:
公共类TestServlet扩展了HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
HttpSession session = req.getSession();
String email = (String)session.getAttribute("ses_email");
Connection cxn;
PreparedStatement ps;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
cxn = DriverManager.getConnection("jdbc:odbc:sm","system","gecbsp");
ps = cxn.prepareStatement("select * from smreg3 where email = ?");
ps.setString(1, email); // this avoids sql injection
ResultSet rs = ps.executeQuery();
if(rs.next()) {
InputStream in = rs.getBinaryStream(1);
byte[] buffer = copyStream(in); // use a library like Apache commons IO here
rs.close(); cxn.close();
resp.setContentType("image/jpeg");
resp.getOutputStream().write(buffer);
resp.flushBuffer();
}
}catch(Exception e){
e.printStackTrace(); // replace this by a proper logging framework
}
}
private byte[] copyStream(InputStream in) {
// just the copy code from your initial example
}
}
之后,您还需要在web.xml中提供Servlet
这样的东西需要进入web.xml的web-app部分:
<servlet-mapping>
<servlet-name>downloadServlet</servlet-name>
<url-pattern>download</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>downloadServlet</servlet-name>
<servlet-class>test.TestServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
然后servlet可以在web-app上下文中下载/下载。