tags:

views:

96

answers:

2

I am trying to open a pdf file using ServletOutputStream in JSP. The code is:

response.setContentLength(statementVO.getOutputStream().size());
response.setContentType("application/pdf");
response.setHeader("Expires", "0");
response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
response.setHeader("Pragma", "public");
ServletOutputStream out = response.getOutputStream();
out.write(statementVO.getOutputStream().toByteArray());
out.flush();
out.close();

I am getting the following error

WAS 6.0 - Response already committed / OutputStream already obtained
A: 

Set response.setHeader("Cache-Control", "max-age=60");

and compare your code with

public class DownloadServlet extends HttpServlet {

    //Initialize global variables
    String fileName="";

    private static Logger log = Logger.getLogger(DownloadServlet.class.getName());

    public void init(ServletConfig config) throws ServletException {
        super.init(config);
    }

    //Process the HTTP Get request
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,
            IOException {
        doPost(request,response);
    }

    //Process the HTTP Post request
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        log.debug("Entered DownloadServlet");
        OutputStream outStream = response.getOutputStream();
        fileName=request.getParameter("fileName");
        log.debug("Filename:"+fileName);
        String filePath = "C:\\test
reference-material";
        File f=new File(filePath, fileName);
        String fileType = fileName.substring(fileName.indexOf(".")+1,fileName.length());
        log.debug("Filetype:"+fileType+";"+f.length());

        if (fileType.trim().equalsIgnoreCase("txt")) {
            response.setContentType( "text/plain" );
        } else if (fileType.trim().equalsIgnoreCase("doc")) {
            response.setContentType( "application/msword" );
        } else if (fileType.trim().equalsIgnoreCase("xls")) {
            response.setContentType( "application/vnd.ms-excel" );
        } else if (fileType.trim().equalsIgnoreCase("pdf")) {
            response.setContentType( "application/pdf" );
            log.debug("content type set to pdf");
        } else {
            response.setContentType( "application/octet-stream" );
        }
response.setHeader("Cache-Control", "max-age=60");
        response.setContentLength((int)f.length());
         response.setHeader("Content-Disposition","attachment; filename=\"SecurityPatterns.pdf\"");

        response.setHeader("Cache-Control", "no-cache");
        byte[] buf = new byte[8192];
        FileInputStream inStream = new FileInputStream(f);
        int sizeRead = 0;
        while ((sizeRead = inStream.read(buf, 0, buf.length)) > 0) {
            log.debug("size:"+sizeRead);
            outStream.write(buf, 0, sizeRead);
        }
        inStream.close();
        outStream.close();

    }

    //Get Servlet information
    public String getServletInfo() {
        return "DownloadServlet Information";
    }
}
org.life.java
+3  A: 

You're getting this error because you're using a JSP file instead of a Java class to write Java code in. The JSP file is intented to serve template text like HTML/CSS/JS and so on. The JSP implicitly uses response.getWriter() to write template text. Whenever you call response.getOutputStream() inside a JSP, you risk getting this error because you cannot open both the Writer and the OutputStream. You can only open the one or the other, see also the linked javadocs.

To solve this problem there are basically 2 solutions:

  1. Do this in a real Java class instead of a JSP file. A Servlet class is the best suitable place for this. You can find here a basic example.

  2. Remove all template text (this includes whitespace and newlines!) from JSP file so that it doesn't implicitly call response.getWriter(). See also this answer for a detailed explanation.

BalusC
Learning from you BalusC :)
org.life.java