无法从portlet获取HttpServletResponse

时间:2013-04-03 05:47:24

标签: jsf portlet httpresponse

我尝试了以下代码:

PortletResponse response1 = (PortletResponse)FacesContext.getCurrentInstance().getExternalContext().getResponse();
        HttpServletResponse response = (HttpServletResponse)response1;

在之前的question中,答案是PortalUtil.getHttpServletResponse(portletResponse) 但问题是response.getOutputStream()上的空指针异常
我的完整代码是

FacesContext facesContext = FacesContext.getCurrentInstance();
        ExternalContext externalContext = facesContext.getExternalContext();
        //here is my code
        PortletResponse portalResponse = (PortletResponse) externalContext.getResponse();
        HttpServletResponse response = PortalUtil.getHttpServletResponse(portalResponse); 

        File file = new File(getFilePath(), getFileName());
        BufferedInputStream input = null;
        BufferedOutputStream output = null;

        try {

            input = new BufferedInputStream(new FileInputStream(file), DEFAULT_BUFFER_SIZE);


            response.reset();
            response.setHeader("Content-Type", "application/pdf");
            response.setHeader("Content-Length", String.valueOf(file.length()));
            response.setHeader("Content-Disposition", "inline; filename=\"" + getFileName() + "\"");
            //here where nullException is returned from response.getOutputStream()
            output = new BufferedOutputStream(response.getOutputStream(), DEFAULT_BUFFER_SIZE);


            byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
            int length;
            while ((length = input.read(buffer)) > 0) {
                output.write(buffer, 0, length);
            }


            output.flush();
        } finally {

            close(output);
            close(input);
        }
        facesContext.responseComplete();

由@BalusC撰写

pdf-handling
我的问题是如何在portlet中获得此响应

0 个答案:

没有答案