我正在使用PF 3.4.2,只是想知道DefaultStreamedContent是否处理关闭已使用的输入流?因为当我尝试在finally块中自己完成它时,它会导致异常。
答案 0 :(得分:3)
Primefaces处理关闭DefaultStreamedContent中使用的流,我检查了fie下载监听器的实现:
public void processAction(ActionEvent actionEvent) throws AbortProcessingException {
FacesContext facesContext = FacesContext.getCurrentInstance();
ELContext elContext = facesContext.getELContext();
StreamedContent content = (StreamedContent) value.getValue(elContext);
if(content == null) {
return;
}
ExternalContext externalContext = facesContext.getExternalContext();
String contentDispositionValue = contentDisposition != null ? (String) contentDisposition.getValue(elContext) : "attachment";
try {
externalContext.setResponseContentType(content.getContentType());
externalContext.setResponseHeader("Content-Disposition", contentDispositionValue + ";filename=\"" + content.getName() + "\"");
externalContext.addResponseCookie(Constants.DOWNLOAD_COOKIE, "true", new HashMap<String, Object>());
byte[] buffer = new byte[2048];
int length;
InputStream inputStream = content.getStream();
OutputStream outputStream = externalContext.getResponseOutputStream();
while ((length = (inputStream.read(buffer))) != -1) {
outputStream.write(buffer, 0, length);
}
externalContext.setResponseStatus(200);
externalContext.responseFlushBuffer();
content.getStream().close();
facesContext.responseComplete();
}
catch(IOException e) {
throw new FacesException(e);
}
}