2016-11-29 2 views
0

저는 primefaces를 사용하여 이미지를 업로드하고 잘라내어 그래픽 이미지에 최종 이미지를 표시합니다.Primefaces graphicImage 스트림이 닫히지 않았습니다. 파일이 잠겼습니다.

프로세스가 정상적으로 작동하지만 문제는 graphicsImage에 표시 할 최종 이미지를 검색 할 때 스트림이 닫히지 않고 파일이 java.exe에 의해 보류되어 문제가 발생한다는 것입니다. 예를 들어 사용자가 로그 아웃 할 때 임시 디렉토리 일 뿐이므로 파일/디렉토리를 삭제합니다.

내 StreamedContent의 게터입니다 : 다음 input.close();을 할 경우

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; 
} 

내가 파일을 삭제할 수,하지만 난이 게터 번 이상이라고 알고 있기 때문에, 표시되지 않습니다 생애주기에.

답변

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); 
    } 
}