I am trying to upload a pdf file in jsf but before upload I would wish to allow user to see preview of the pdf before hitting submit button but its failing and giving this error message in chrome
and is the stack trace I get when I try preview pdf using chrome browser
24-Feb-2021 20:29:57.256 SEVERE [http-nio-8084-exec-255] com.sun.faces.context.AjaxExceptionHandlerImpl.handlePartialResponseError java.io.FileNotFoundException: demo_receipt.pdf (The system cannot find the file specified)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at co.sf.controller.UploadController.load_stream_Content(UploadController.java:129)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.el.parser.AstValue.invoke(AstValue.java:247)
and this is my managed bean code
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package co.sf.controller;
//imports
@ManagedBean(name = "userData", eager = true)
@SessionScoped
public class UploadController implements Serializable {
public UploadController() {
}
StreamedContent streamedContent;
public void load_stream_Content() throws Exception {
Path fileName = Paths.get(image.getSubmittedFileName());
String path= fileName.toString();
FacesContext context = FacesContext.getCurrentInstance();
if (context.getCurrentPhaseId() == PhaseId.RENDER_RESPONSE) {
streamedContent = new DefaultStreamedContent();
} else {
String fs = Paths.get(image.getSubmittedFileName()).getFileName().toString();
streamedContent=new DefaultStreamedContent(new FileInputStream(new File(path)), "application/pdf", fs);//error is occuring here
}
streamed = true;
}
boolean streamed = false;
public boolean isStreamed() {
return streamed;
}
and here is my jsf page
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:p="http://primefaces.org/ui">
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
<h:form enctype="multipart/form-data">
<div id="upload-button">
<p>Browse</p>
<h:inputFile value="#{userData.image}">
<f:passThroughAttribute name="accept" value="*.pdf"/>
<f:ajax event="change" listener="#{userData.load_stream_Content}" render=":pdf_preview"/>
</h:inputFile>
</div>
<h:commandButton value="Upload" action="#{userData.doUpload()}">
<f:ajax execute="@form" render="@form"/>
</h:commandButton>
</h:form>
<p:media id="pdf_preview" rendered="#{userData.streamed}" width="100%" height="700px" value="#{userData.streamedContent}"
cache="false" player="pdf"/>
</h:body>
</html>