本文介绍了流关闭异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我要保存上载的图像时得到Stream is closed Exception.我正在尝试在保存之前预览上载图像的graphicImage.此操作正在运行.但是我无法保存图像.这是我的代码:

I'm getting Stream is closed Exception when I'm going to save the uploaded image.I'm tring to preview graphicImage of uploaded image the before save. This operation is working. But I can't save the image. Here is my code:

private InputStream in;
private StreamedContent filePreview;
// getters and setters

public void upload(FileUploadEvent event)throws IOException {
    // Folder Creation for upload and Download
    File folderForUpload = new File(destination);//for Windows
    folderForUpload.mkdir();
    file = new File(event.getFile().getFileName());
    in = event.getFile().getInputstream();
    filePreview = new DefaultStreamedContent(in,"image/jpeg");
    FacesMessage msg = new FacesMessage("Success! ", event.getFile().getFileName() + " is uploaded.");
    FacesContext.getCurrentInstance().addMessage(null, msg);
}

public void setFilePreview(StreamedContent fileDownload) {
    this.filePreview = fileDownload;
}

public StreamedContent getFilePreview() {
    return filePreview;
}

public void saveCompanyController()throws IOException{
    OutputStream out  = new FileOutputStream(getFile());
    byte buf[] = new byte[1024];
    int len;
    while ((len = in.read(buf)) > 0)
        out.write(buf, 0, len);
    FileMasterDO fileMasterDO=new FileMasterDO();
    fileMasterDO.setFileName(getFile().getName());
    fileMasterDO.setFilePath(destination +file.getName());
    fileMasterDO.setUserMasterDO(userMasterService.findUserId(UserBean.getUserId()));
    fileMasterDO.setUpdateTimeStamp(new Date());
    in.close();
    out.flush();
    out.close();
    fileMasterService.save(filemaster);
}

bean在会话范围内.

The bean is in the session scope.

推荐答案

您尝试读取一次InputStream两次(第一次是在上载方法的DefaultStreamedContent构造函数中,第二次是在复制循环中保存方法).这是不可能的.只能读取一次.您需要先将其读入byte[],然后将其分配为bean属性,以便可以将其重新用于StreamedContent和保存.

You're trying to read an InputStream twice (the first time is in DefaultStreamedContent constructor of upload method and the second time is in the copy loop of the save method). This is not possible. It can be read only once. You need to read it into a byte[] first and then assign it as a bean property so that you can reuse it for both the StreamedContent and the save.

确保从不将外部资源(例如InputStreamOutputStream)作为bean属性保存.将它们全部从当前和其他bean(如果适用)中删除,并使用byte[]将图像的内容作为属性保存.

Make sure that you never hold external resources such as InputStream or OutputStream as a bean property. Remove them all from the current and other beans where applicable and use byte[] to hold the image's content as property.

在特定情况下,您需要按以下步骤对其进行修复:

In your particular case, you need to fix it as follows:

private byte[] bytes; // No getter+setter!
private StreamedContent filePreview; // Getter only.

public void upload(FileUploadEvent event) throws IOException {
    InputStream input = event.getFile().getInputStream();

    try {
        IOUtils.read(input, bytes);
    } finally {
        IOUtils.closeQuietly(input);
    }

    filePreview = new DefaultStreamedContent(new ByteArrayInputStream(bytes), "image/jpeg");
    // ...
}

public void saveCompanyController() throws IOException {
    OutputStream output = new FileOutputStream(getFile());

    try {
        IOUtils.write(bytes, output);
    } finally {
        IOUtils.closeQuietly(output);
    }

    // ...
}

注意:IOUtils来自Apache Commons IO,您应该已经将它放在类路径中,因为它是<p:fileUpload>的依赖项.

Note: IOUtils is from Apache Commons IO, which you should already have in the classpath as it's a dependency of <p:fileUpload>.

这篇关于流关闭异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 06:37