2012-05-16 56 views
0

我在我的web应用程序中使用struts2。我想在jsp上渲染pdf流。我目前这样做:使用struts2在jsp上渲染pdf流

public String renderPDF() throws Exception 
{ 
    myService.rederPDF(getServletResponse().getServletOutputStream()); 
    return SUCCESS; 
} 

myService的rederPDF方法获取pdf流并写入到servlet响应输出流。但是这引发了一个异常,“响应已经被提交”。

+2

为什么不使用流结果和输入流更干净和更好? –

+0

我以为流结果只用于文件下载。它可以用来在页面上呈现PDF流吗? –

+1

流结果用于打开输入流,我相信这可以使用上述结果类型 –

回答

0

在转发之前,您已经向客户端发送了一些内容时会发生此异常。

我正在使用Servlets下载文件。看一看。

public class FileDownloadServlet extends HttpServlet { 

public void doPost(HttpServletRequest request, HttpServletResponse response) 
     throws ServletException, IOException { 

    byte[] b = null; 
    ServletOutputStream sop = null; 

    try { 
     response.setContentType("application/pdf"); 
     response.setHeader("Content-Disposition","attachment; filename=yourFileName.pdf");   

     sop = response.getOutputStream(); 
     b = myService.getFileData(); /* your code */ 
     sop.write(b);  
     return;   
    } catch (Exception e) { 
     /* some code*/ 
    } 
    finally{ 
     /* some code*/ 
    } 
} 
} 
+0

我真的不想下载此文件。我想在一个页面上呈现它。我正在使用struts 2. –

+0

渲染意味着,您想要将该文件与其他页面内容一起显示为PDF视图? –

+0

是的。这正是我想要做的 –