public static String getContentType(Resource resource, ExternalContext externalContext) {
    String contentType = resource.getContentType();

    // the resource does not provide a content-type --> determine it via
    // mime-type
    if (contentType == null || contentType.length() == 0) {
      String resourceName = getWrappedResourceName(resource);

      if (resourceName != null) {
        contentType = externalContext.getMimeType(resourceName);
      }
    }

    return contentType;
  }
 public void download(String namePublicationSelected) {
   Publication publicationSelected =
       this.managePublicationsService.retrieveByName(namePublicationSelected);
   String filepath = publicationSelected.getFilepath();
   File file = new File(filepath);
   FileInputStream input = null;
   try {
     input = new FileInputStream(file);
   } catch (FileNotFoundException e) {
     e.printStackTrace();
   }
   ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
   setFileDownload(
       new DefaultStreamedContent(
           input, externalContext.getMimeType(file.getName()), file.getName()));
 }
  public void download(String fileName) {
    for (ServerInfo info : servers) {
      try (Socket server = info.getSocket();
          OutputStream os = server.getOutputStream();
          PrintWriter pw = new PrintWriter(os);
          InputStream is = server.getInputStream()) {

        /*Protocol*/
        os.write(Protocol.DOWNLOAD);
        pw.println(fileName);
        pw.flush();

        /*Check if file exists, 1 if present, 0 if otherwise*/
        int reply = is.read();
        if (reply == 1) {

          /*Response header*/
          FacesContext fc = FacesContext.getCurrentInstance();
          ExternalContext ec = fc.getExternalContext();
          ec.responseReset();
          ec.setResponseContentType(ec.getMimeType(fileName));
          ec.setResponseHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");

          /*Write bytes*/
          transferBytes(is, ec.getResponseOutputStream());
          fc.responseComplete();
          System.out.println(
              "File " + fileName + " was successfully downloaded with size " + file.getSize());
          break;
        }
      } catch (ConnectException e) {
        // Socket is closed
        System.out.println(e + " download at " + info);
      } catch (IOException ex) {
        Logger.getLogger(ClientController.class.getName()).log(Level.SEVERE, null, ex);
      }
    }
  }