When I click the h:commandButton
it execute the myBean.dowanlod()
method, but it doesn't download any files.
Here is my methods in the backing bean. No exceptions. Cursor gets busy and seems like waiting for a response. Are there any additional configurations for this kind of operations or is there any wrong with this code?
<h:commandButton value="download" action="#{myBean.download()}" />
@ManagedBean
@SessionScoped
public class MyBean implements Serializable{
//....
public String download{
FacesContext facesContext = FacesContext.getCurrentInstance();
ExternalContext externalContext = facesContext.getExternalContext();
String fileName = "test.txt";
String filePath = "D:\\test.txt"; //externalContext.getRealPath("") + File.separator + fileName;
File file = new File(filePath);
externalContext.setResponseHeader("Content-Type", "text/plain");
externalContext.setResponseHeader("Content-Length", String.valueOf(file.length()));
externalContext.setResponseHeader("Content-Disposition", "attachment;filename=\"" + fileName + "\"");
InputStream input = null;
OutputStream output = null;
try {
input = new FileInputStream(file);
output = externalContext.getResponseOutputStream();
IOUtils.copy(input, output);
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
} finally {
IOUtils.closeQuietly(output);
IOUtils.closeQuietly(input);
}
facesContext.responseComplete();
return null;
}
//...
}