<h:commandLink action="#{logController.downloadMessage}"> <h:outputText styleClass="fa fa-fw fa-download"/> <f:setPropertyActionListener value="#{log}" target="#{logController.selectedLog}"/> </h:commandLink>
public void downloadMessage() throws IOException { logger.info("downloading the log file"); File file = new File(searchKeyword + "_log.txt"); FileUtils.writeStringToFile(file, selectedLog.getMessageContent(), Charset.defaultCharset()); FacesContext facesContext = FacesContext.getCurrentInstance(); HttpServletResponse response = (HttpServletResponse) facesContext.getExternalContext().getResponse(); response.setHeader("Content-Disposition", "attachment;filename=" + file.getName()); response.setContentLength((int) file.length()); FileInputStream input = null; try { int i = 0; input = new FileInputStream(file); byte[] buffer = new byte[1024]; while ((i = input.read(buffer)) != -1) { response.getOutputStream().write(buffer); response.getOutputStream().flush(); } facesContext.responseComplete(); facesContext.renderResponse(); } catch (IOException e) { logger.error(e.getMessage()); } finally { try { if (input != null) { input.close(); } } catch (IOException e) { logger.error(e.getMessage()); } } }
d