Code: Select all
<p:commandButton value="download" process="@this">
<f:param name="fileName" value="#{resourceBean.fileName}" />
<f:param name="filePath" value="#{resourceBean.filePath}" />
<p:fileDownload value="#{fileDownloadBean.file}" />
</p:commandButton>
Code: Select all
@Named
@RequestScoped
public class FileDownloadBean {
private StreamedContent file;
public FileDownloadBean() throws ServiceException {
String fileName = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("fName");
String filePath = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("filePath");
try {
FileInputStream fis= new FileInputStream(filePath);
file = DefaultStreamedContent.builder().name("fileName ").contentType(URLConnection.guessContentTypeFromName("fileName .html"))
.stream(() -> fis).build();
} catch (FileNotFoundException fnf) {
throw fnf;
}
}
public StreamedContent getFile() {
return file;
}
}