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

时间:2015-05-20 14:35:11

标签: java hibernate jsp spring-mvc

我可以通过此代码找到所有数据

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();

我正在使用以下代码从数据库中获取数据...但我的网页变为空白,我在控制台中遇到了一些异常..

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

我的控制台出现异常......

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

java.lang.IllegalStateException:已为此响应调用了getOutputStream()     在org.apache.catalina.connector.Response.getWriter(Response.java:604)     在org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:198)     在org.apache.jasper.runtime.JspWriterImpl.initOut(JspWriterImpl.java:125)     在org.apache.jasper.runtime.JspWriterImpl.flushBuffer(JspWriterImpl.java:118)     在org.apache.jasper.runtime.JspWriterImpl.write(JspWriterImpl.java:326)     at org.apache.jasper.runtime.JspWriterImpl.write(JspWriterImpl.java:342)     在org.apache.jsp.allpartners_jsp._jspService(allpartners_jsp.java:318)     在org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)     在javax.servlet.http.HttpServlet.service(HttpServlet.java:803)     在org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:393)     在org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)     在org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)     在javax.servlet.http.HttpServlet.service(HttpServlet.java:803)     在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)     在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)     at 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)

2 个答案:

答案 0 :(得分:1)

在响应已经开始写回调用者之后,不应该调用response.setContentType()

在调用setContentType之前尝试调用getOutputStream

如果这样做无效,您是否可以检查代码中可能会调用responseresponse.getOuputStream的代码?这样你就会知道哪些代码开始写回浏览器了。

<强>更新

一旦开始写回复。您现在可以呈现JSP。 如果这是一个servlet代码,你可以“返回”而不必转发到JSP。

答案 1 :(得分:0)

OutputStream output = response.getOutputStream();  

response.setContentType("image/jpeg");

正如您所看到的,您首先获取响应并稍后设置其类型,这可能会导致问题。

尝试纠正这个&amp;如果事情仍然很糟糕,也可以发布堆栈跟踪。