Advertisement
Guest User

Untitled

a guest
Oct 8th, 2016
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. public void downloadFile() {
  2. File file = new File("/tmp/Report.pdf");
  3. HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance()
  4. .getExternalContext().getResponse();
  5. response.setHeader("Content-Disposition", "attachment;filename=file.txt");
  6. response.setContentLength((int) file.length());
  7. ServletOutputStream out = null;
  8. try {
  9. FileInputStream input = new FileInputStream(file);
  10. byte[] buffer = new byte[1024];
  11. out = response.getOutputStream();
  12. int i = 0;
  13. while ((i = input.read(buffer)) != -1) {
  14. out.write(buffer);
  15. out.flush();
  16. }
  17. FacesContext.getCurrentInstance().getResponseComplete();
  18. } catch (IOException err) {
  19. err.printStackTrace();
  20. } finally {
  21. try {
  22. if (out != null) {
  23. out.close();
  24. }
  25. } catch (IOException err) {
  26. err.printStackTrace();
  27. }
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement