How To Download A File From The Server Using Javaee

The next snippet volition conduct maintain a filename inward the server's directory, ready it equally the content of the FacesContext for the user to download.

public String downloadXMLInvoice(String fileName) {  File file = novel File(getXmlInvoiceDir().getAbsolutePath() + File.separator + fileName);    displace {   FacesContext context = FacesContext.getCurrentInstance();   HttpServletResponse res = (HttpServletResponse) context.getExternalContext().getResponse();   res.setContentType("application/force-download");   res.setContentLength((int) file.length());   res.addHeader("Content-disposition", "attachment;filename=\"" + fileName + "\"");    OutputStream out = res.getOutputStream();   InputStream fin = novel FileInputStream(file);    byte[] buf = novel byte[1024];   int sig = 0;   piece ((sig = fin.read(buf, 0, 1024)) != -1) {    out.write(buf, 0, sig);   }      fin.close();   out.flush();   out.close();      context.responseComplete();  } grab (Exception e) {   log.error(Epic failed :-) ", e.getMessage(), file.getAbsolutePath());  }    furnish null; } 
Next
Previous
Click here for Comments

0 komentar:

Please comment if there are any that need to be asked.