2016-11-29 10 views
0

私はprimefacesを使用してイメージをアップロードし、それをトリミングしてから、最終イメージをgraphicImageに表示します。Primfaces graphicImageストリームがクローズされていない、ファイルがロックされている

プロセスは問題なく動作しますが、問題は、imageImageに表示する最終イメージを取得するときにストリームが閉じられず、ファイルがjava.exeによって保持されているためですユーザーがログアウトしたときなどにファイル/ディレクトリを削除します。これは単に一時ディレクトリであるためです。

これは私のStreamedContentのゲッターです:

public StreamedContent getGraphicCropped() { 
    try{ 
     if (newImageName != null) { 
      File file2 = new File(pathCroppedImage); 
      InputStream input = new FileInputStream(file2); 
      graphicCropped = new DefaultStreamedContent(input); 
      showImageFinal = true; 
     } 

    } catch(Exception e){ 
     e.printStackTrace(); 
    } 

    return graphicCropped; 
} 

私はこのゲッターが複数回呼び出されたことを知っているので、私は、input.close();その後、私は、ファイルを削除することができるよん、しかし、それが表示されていない場合ライフサイクルで

答えて

0

私はStreamedContentの提案ゲッターを使用して、それを解決してきました:

public StreamedContent getGraphicCropped() throws FileNotFoundException { 

    FacesContext context = FacesContext.getCurrentInstance(); 

    if (context.getCurrentPhaseId() == PhaseId.RENDER_RESPONSE) { 
     // So, we're rendering the HTML. Return a stub StreamedContent so that it will generate right URL. 
     return new DefaultStreamedContent(); 
    } 
    else { 
     // So, browser is requesting the image. Return a real StreamedContent with the image bytes. 
     File file2 = new File(pathCroppedImage); 
     InputStream input = new FileInputStream(file2); 
     showImageFinal = true; 
     return new DefaultStreamedContent(input); 
    } 
} 
関連する問題