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