次のコードを試しました:
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によって書かれています
私の質問は、ポートレットでこの応答を取得する方法です