-1

我尝试在jsp中下载生成的pdf。但我的日志显示以下错误。我怎样才能避免这个错误。我的代码和错误如下

List<SDO> sdolist = new ArrayList<SDO>();   
    try{
        sdolist = TravelAdvanceRequest.generatePDF(objSession,objList); //calling service
        infoMsg="Pdf Generation Success";
        if (sdolist != null) {
            StringType stringMsg = (StringType) sdolist.get(0);
            infoMsg="pdf generation success......";
            System.out.println("pdf generation success......");
            System.out.println("file path===========>>"+stringMsg.getString());
            ServletOutputStream op          =response.getOutputStream();

         //throw new IllegalStateException("MyException");
        String filename = stringMsg.getString() == null ? "" : stringMsg.getString();
        File                f           =   new File(filename);         
        int                 length      =   0;

        ServletContext      context     =   getServletConfig().getServletContext();
        String              mimetype    =   context.getMimeType( filename );
        if(f.isFile()){
            response.setContentType( (mimetype != null) ? mimetype : "application/octet-stream" ); 
            response.setContentLength( (int)f.length() );
            response.setHeader( "Content-Disposition", "attachment; filename=\"Axis template for "+strCurrency+" account.pdf\"" );
            byte[] bbuf = new byte[1000];
            DataInputStream in = new DataInputStream(new FileInputStream(f));

       while ((in != null) && ((length = in.read(bbuf)) != -1))
            {
                op.write(bbuf,0,length);
            }

            in.close();
            response.getOutputStream().flush();
           // op.flush();
            op.close(); 
            return;
        }else{
            System.out.println("Exception throws there is no such file in the Directory : "+filename);
        }

    }

    }catch(IllegalStateException e1)
    {
        System.out.println("Illegal State Exception......"+e1.getMessage());    
    }
    catch(Exception e)
    {
        System.out.println("Error in Pdf Creation......"+e.getMessage());
        errMsg=e.getMessage();

    }

java.lang.IllegalStateException: getOutputStream() has already been called for this response
    at org.apache.catalina.connector.Response.getWriter(Response.java:619)
    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.PageContextImpl.release(PageContextImpl.java:188)
    at org.apache.jasper.runtime.JspFactoryImpl.internalReleasePageContext(JspFactoryImpl.java:118)
    at org.apache.jasper.runtime.JspFactoryImpl.releasePageContext(JspFactoryImpl.java:77)
    at org.apache.jsp.DebitDetails_jsp._jspService(DebitDetails_jsp.java:374)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:369)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:322)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:249)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.tuscany.sca.host.webapp.TuscanyServletFilter.doFilter(TuscanyServletFilter.java:103)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:235)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:190)
    at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:92)
    at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.process(SecurityContextEstablishmentValve.java:126)
    at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:70)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:158)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:330)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:829)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:598)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
    at java.lang.Thread.run(Unknown Source)
4

1 回答 1

1

您收到此错误的原因是您正在使用 JSP 执行逻辑。JSP 使用 的Writer对象呈现其输出response,但您也在getOutputStreamJSP 内部调用。因此,由于您尝试同时使用 theOutputStream和 the ,因此您会遇到异常Writer。您还没有显示您的整个 JSP 代码,但我猜您在主要逻辑之后正在执行一些输出(可能是一些空白字符)。正如@VigneshVino 所说,您应该在 servlet 中执行您的逻辑。JSP 应该只用于呈现视图。

于 2013-05-16T05:16:37.043 回答