简体   繁体   English

如何使用Hibernate和Spring MVC从数据库在Jsp页面上显示图像

[英]How to display image on Jsp page from database using Hibernate and spring MVC

I am able to find all the data through this code 我可以通过此代码找到所有数据

while(it.hasNext())
        {
        Object objnew=it.next();
        PartnerRegistrationIndividual PartRegIndv =(PartnerRegistrationIndividual) objnew;

        pid=PartRegIndv.getId();
        firstname=PartRegIndv.getFname();
        lastname=PartRegIndv.getLname();
        email=PartRegIndv.getEmail();
        mobile=PartRegIndv.getMobile();
        foe=PartRegIndv.getSpeciality();
        expSalPerDay =PartRegIndv.getExpectedSalaryPerDay();
        expSalPerMonth=PartRegIndv.getExpectedSalaryPerMonth();
        current_status=PartRegIndv.getApproval_status();

I am using following code to get the data from database...but my webpage goes blank and i get some exception in console.. 我正在使用以下代码从数据库获取数据...但是我的网页空白了,控制台出现异常。

        Blob imgdata=PartRegIndv.getImage();
        imgdata.getBinaryStream();
        OutputStream output = response.getOutputStream();
        response.setContentType("image/jpeg");
        response.getOutputStream().flush();
        response.getOutputStream().close();

Exception which comes in my console... 我的控制台中出现异常...

SEVERE: Servlet.service() for servlet emen threw exception

java.lang.IllegalStateException: getOutputStream() has already been called for this response at org.apache.catalina.connector.Response.getWriter(Response.java:604) at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:198) 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.JspWriterImpl.write(JspWriterImpl.java:326) at org.apache.jasper.runtime.JspWriterImpl.write(JspWriterImpl.java:342) at org.apache.jsp.allpartners_jsp._jspService(allpartners_jsp.java:318) 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.ht java.lang.IllegalStateException:已在org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade)的org.apache.catalina.connector.Response.getWriter(Response.java:604)处为此响应调用getOutputStream()。 java:198)位于org.apache.jasper.runtime.JspWriterImpl.initOut(JspWriterImpl.java:125)位于org.apache.jasper.runtime.JspWriterImpl.flushBuffer(JspWriterImpl.java:118)位于org.apache.jasper.runtime org.apache.jsp.allpartners_jsp._jspService(allpartners_jsp.java:318)上的org.apache.jasper.runtime.JspWriterImpl.write(JspWriterImpl.java:342)上的。位于org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java)的javax.servlet.http.HttpServlet.service(HttpServlet.java:803)的apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70): 393)在org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)在org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)在javax.servlet.ht tp.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.ApplicationDispatcher.invoke(ApplicationDispatcher.java:654) at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:445) at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:379) at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:292) org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)处的tp.HttpServlet.service(HttpServlet.java:803)org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)在org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:654)在org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:445)在org.apache.catalina.core.ApplicationDispatcher.doForward (ApplicationDispatcher.java:379)在org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:292)

The response.setContentType() shouldn't be called after response has already started to be written back to the caller. 在响应已经开始写回调用方之后,不应调用response.setContentType()

Try to invoke setContentType before you invoke getOutputStream . 在调用getOutputStream之前,请尝试调用setContentType

If that doesn't help, could you check in your code where response or response.getOuputStream might be getting called? 如果这样做没有帮助,您可以在可能会调用responseresponse.getOuputStream地方检入代码吗? That way you will know what piece of code started writing back to the browser. 这样,您将知道哪些代码开始写回到浏览器。

UPDATE UPDATE

Once you start writing to the response. 一旦开始写响应。 You are now allowed to render a JSP. 现在允许您呈现JSP。 If this was a servlet code, you could just "return" without having to forward to a JSP. 如果这是一个servlet代码,则可以“返回”,而不必转发到JSP。

OutputStream output = response.getOutputStream();  

response.setContentType("image/jpeg");

As you can see, you are fetching response first and setting it's type later, which might be causing the issue. 如您所见,您首先要获取响应,然后再设置其类型,这可能是导致问题的原因。

Try correcting this & if things are still nasty, post stacktrace also. 尝试更正此问题,如果情况仍然令人讨厌,请同时发布stacktrace。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM