1

我尝试了以下代码:

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


在上一个问题中,答案是,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 中得到这个响应

4

0 回答 0