this work for me
My file in folder path
WEB-INF\pages\forms
cod in xhtml
file
<h:commandLink value="Download" action="#{formBean.downloadFile('FileName.doc')}" styleClass="link"/>
beans
public String downloadFile(String fileName) {
try {
FacesContext facesContext = FacesContext.getCurrentInstance();
HttpServletResponse response = ((HttpServletResponse) facesContext.getExternalContext().getResponse());
String reportPath = facesContext.getExternalContext().getRealPath("\\pages\\forms") + File.separator + fileName;
response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName+ "\"");
File file = new File(reportPath);
BufferedInputStream bIn = new BufferedInputStream(new FileInputStream(file));
int rLength = -1;
byte[] buffer = new byte[1000];
while ((rLength = bIn.read(buffer, 0, 100)) != -1) {
response.getOutputStream().write(buffer, 0, rLength);
}
FacesContext.getCurrentInstance().responseComplete();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}