Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void downloadFile() {
- File file = new File("/tmp/Report.pdf");
- HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance()
- .getExternalContext().getResponse();
- response.setHeader("Content-Disposition", "attachment;filename=file.txt");
- response.setContentLength((int) file.length());
- ServletOutputStream out = null;
- try {
- FileInputStream input = new FileInputStream(file);
- byte[] buffer = new byte[1024];
- out = response.getOutputStream();
- int i = 0;
- while ((i = input.read(buffer)) != -1) {
- out.write(buffer);
- out.flush();
- }
- FacesContext.getCurrentInstance().getResponseComplete();
- } catch (IOException err) {
- err.printStackTrace();
- } finally {
- try {
- if (out != null) {
- out.close();
- }
- } catch (IOException err) {
- err.printStackTrace();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement