0

I want to upload an image and show it on the same page after uploading using JSF2.2 (Mojarra 2.2.5), WildFly 8.0.0.Final, and PrimeFaces 6.0.

The file gets uploaded successfully, I am also saving it additionally in a temprarily folder outside of the application's server.

However, I am getting the following excpetion when trying to display it:

04:53:12,680 SEVERE [org.primefaces.application.resource.StreamedContentHandler] (default task-4) Error in streaming dynamic resource. null

The exception occurs at line 70 of

org.primefaces.application.resource.StreamedContentHandler.handle() 

as shown below:

exception row exception handling

Unfortunately, existing posts did not help me solve my problem:

Error in streaming dynamic resource. null

Display dynamic image from database with p:graphicImage and StreamedContent

So, I need help!

This an excerpt from my xhtml:

 <p:fileUpload  id="fileUploader" fileUploadListener="#{assignmentBean.upload}" auto="true" dragDropSupport="true" update="graphicImage"
                               sizeLimit="10000000" fileLimit="3" allowTypes="/(\.|\/)(gif|jpe?g|png)$/" rendered="#{assignmentBean.existing}"/>


<p:graphicImage id="graphicImage" width = "50" height="50" value="#{assignmentBean.uploadedFileAsStream}" /> <!--  rendered="#{not empty assignmentBean.uploadedFileAsStream}" /> -->

This is an excerpt from my backing bean:

@ViewScoped
@ManagedBean(name = "assignmentBean")
public class AssignmentBean implements Serializable {



public static final String UPLOAD_FILE_PATH = "C:/Temp/IamOKYouAreOK";

public void upload(FileUploadEvent event) {

    if ((this.uploadedFile = event.getFile()) == null) 
        return;

    try(InputStream input = this.uploadedFile.getInputstream()){

        String filename = FilenameUtils.getBaseName(uploadedFile.getFileName()); 
        String extension = FilenameUtils.getExtension(uploadedFile.getFileName());

        Path targetFile = Paths.get(getUploadSavePath() + "/" + filename + "." + extension);
        Files.copy(input, targetFile, StandardCopyOption.REPLACE_EXISTING);

        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Successful", event.getFile().getFileName() + " is uploaded"));

    } catch (IOException e1) {

        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "ERROR I/O", "Code of the error: DC1"));
        e1.printStackTrace();
        }

}




  private String getUploadSavePath(){

        return  UPLOAD_FILE_PATH + "/" + "A_" + this.assId;
    }


public StreamedContent getUploadedFileAsStream() throws IOException {

    FacesContext context = FacesContext.getCurrentInstance();

    if (this.uploadedFile != null) {

        String filename = FilenameUtils.getBaseName(uploadedFile.getFileName()); 
        String extension = FilenameUtils.getExtension(uploadedFile.getFileName());

        Path copiedFile = Paths.get(getUploadSavePath() + "/" + filename + "." + extension);

        ByteArrayContent fileContent = new ByteArrayContent(Files.readAllBytes(copiedFile), "img/" + extension);

        return fileContent;

    }

    return null;
}

}

Community
  • 1
  • 1
Alex Mi
  • 1,409
  • 2
  • 21
  • 35
  • One of the duplicates which you found has this boldfaced: *"never declare StreamedContent nor any InputStream as a bean property; only create it brand-new in the getter of a stateless @ApplicationScoped bean when the webbrowser actually requests the image content."* Yet you're still attempting to reference the file content as view scoped bean property. This is never going to work as extensively elaborated in the duplicate. – BalusC Sep 26 '16 at 06:42
  • [link](http://stackoverflow.com/users/157882/balusc) thank you for your help! I implemented a slightly modified version of the solution you suggested and it works. – Alex Mi Oct 02 '16 at 14:28
  • @BalusC thank you for your help! I implemented a slightly modified version of the solution you suggested and it works. – Alex Mi Oct 02 '16 at 14:35
  • @BalusC For some reason, I am not able to pass neither request parameters, nor RequestAttributes to the ApplicationScoped bean. Using Flash parameters also did not solve my problem. The only thing which worked for me, was setting a session attribute in the backing bean upon uploading the file (i.e. the name and the path to the uploaded file) and obtaining these session attributes upon reading the input stream in the application scoped bean. Any idea why this is so? – Alex Mi Oct 04 '16 at 03:22

0 Answers0