流关闭FileInputStream

时间:2018-10-09 00:38:42

标签: java jsf primefaces

我有这个问题 我尝试使用这种方法下载文件,但是当我尝试下载文件时,会引发此错误。

    public void prepDownload(String noIM, String ejerc, String ubicFi, 
    Integer opc) throws Exception 
    {
    direArch = "";

    if (opc == 1) {
        direArch = ubicFi + ejerc + "\\" + noIM + "\\" + noIM + "-" + ejerc + ".pdf";
    }
    System.out.println(direArch);
    File file = new File(direArch);
    InputStream input = null;
    try {
        input = new FileInputStream(file);
        ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
        setDownload(new DefaultStreamedContent(input, externalContext.getMimeType(file.getName()), file.getName()));

    } catch (IOException e) {


        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "

  the file can not be downloaded", ""));

    } finally{
        if (input!=null){
            input.close();
        }

    }
}


public void setDownload(DefaultStreamedContent download) {
    this.download = download;

}

public DefaultStreamedContent getDownload() throws Exception {

    return download;

}

堆栈跟踪

    java.io.IOException: Stream Closed
    at java.io.FileInputStream.readBytes(Native Method)
    at java.io.FileInputStream.read(FileInputStream.java:233)
    at org.primefaces.component.filedownload.
    FileDownloadActionListener.processAction
    (FileDownloadActionListener.java:90)

我已经尝试过首先验证文件是否存在而没有任何结果。

我主要想知道原因是否是由于input.close()的位置引起的。还有其他关闭方式吗?

0 个答案:

没有答案