3

我正在使用 PF 3.4.2,只想知道 DefaultStreamedContent 是否处理关闭使用的输入流?因为当我尝试自己在 finally 块中执行此操作时,会导致异常。

4

1 回答 1

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);
            }
    }
于 2013-11-13T16:40:33.953 回答