Cómo proporcionar una descarga de archivos desde un JSF backing bean?


¿Hay alguna forma de proporcionar una descarga de archivos desde un método de acción de JSF backing bean? He intentado muchas cosas. El problema principal es que no puedo calcular cómo obtener el OutputStream de la respuesta para escribir el contenido del archivo. Sé cómo hacerlo con un Servlet, pero esto no se puede invocar desde un formulario JSF y requiere una nueva solicitud.

¿Cómo puedo obtener el OutputStream de la respuesta del FacesContext actual?

Author: BalusC, 2012-02-22

4 answers

Introducción

Usted puede conseguir todo a través de ExternalContext. En JSF 1.x, puede obtener el objeto raw HttpServletResponse por ExternalContext#getResponse(). En JSF 2.x, puede utilizar el montón de nuevos métodos de delegado como ExternalContext#getResponseOutputStream() sin la necesidad de agarrar el HttpServletResponse de debajo de las capuchas de JSF.

En la respuesta, debe establecer el encabezado Content-Type para que el cliente sepa qué aplicación asociar con el archivo proporcionado. Y, debe establecer el encabezado Content-Length para que el cliente pueda calcule el progreso de la descarga, de lo contrario será desconocido. Y, debe establecer el encabezado Content-Disposition a attachment si desea un diálogo Guardar como, de lo contrario el cliente intentará mostrarlo en línea. Finalmente, simplemente escriba el contenido del archivo en el flujo de salida de respuesta.

La parte más importante es llamar FacesContext#responseComplete() para informar a JSF que no debe realizar la navegación y la representación después de haber escrito el archivo en la respuesta, de lo contrario el final de la respuesta será contaminado con el contenido HTML de la página, o en versiones anteriores de JSF, obtendrá un IllegalStateException con un mensaje como getoutputstream() has already been called for this response cuando la implementación de JSF llame a getWriter() para renderizar HTML.

Apague ajax / no use comando remoto!

Solo necesita asegurarse de que el método de acción es no llamado por una solicitud ajax, sino que es llamado por una solicitud normal mientras dispara con <h:commandLink> y <h:commandButton>. Las solicitudes Ajax y los comandos remotos son manejados por JavaScript que a su vez tiene, por razones de seguridad, no hay facilidades para forzar un diálogo Excepto como con el contenido de la respuesta ajax.

En caso de que esté utilizando, por ejemplo, PrimeFaces <p:commandXxx>, entonces debe asegurarse de desactivar explícitamente ajax a través del atributo ajax="false". En caso de que esté usando ICEfaces, entonces necesita anidar un <f:ajax disabled="true" /> en el componente de comando.

JSF genérico 2.x ejemplo

public void download() throws IOException {
    FacesContext fc = FacesContext.getCurrentInstance();
    ExternalContext ec = fc.getExternalContext();

    ec.responseReset(); // Some JSF component library or some Filter might have set some headers in the buffer beforehand. We want to get rid of them, else it may collide.
    ec.setResponseContentType(contentType); // Check http://www.iana.org/assignments/media-types for all types. Use if necessary ExternalContext#getMimeType() for auto-detection based on filename.
    ec.setResponseContentLength(contentLength); // Set it with the file size. This header is optional. It will work if it's omitted, but the download progress will be unknown.
    ec.setResponseHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\""); // The Save As popup magic is done here. You can give it any file name you want, this only won't work in MSIE, it will use current request URL as file name instead.

    OutputStream output = ec.getResponseOutputStream();
    // Now you can write the InputStream of the file to the above OutputStream the usual way.
    // ...

    fc.responseComplete(); // Important! Otherwise JSF will attempt to render the response which obviously will fail since it's already written with a file and closed.
}

JSF genérico 1.x ejemplo

public void download() throws IOException {
    FacesContext fc = FacesContext.getCurrentInstance();
    HttpServletResponse response = (HttpServletResponse) fc.getExternalContext().getResponse();

    response.reset(); // Some JSF component library or some Filter might have set some headers in the buffer beforehand. We want to get rid of them, else it may collide.
    response.setContentType(contentType); // Check http://www.iana.org/assignments/media-types for all types. Use if necessary ServletContext#getMimeType() for auto-detection based on filename.
    response.setContentLength(contentLength); // Set it with the file size. This header is optional. It will work if it's omitted, but the download progress will be unknown.
    response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\""); // The Save As popup magic is done here. You can give it any file name you want, this only won't work in MSIE, it will use current request URL as file name instead.

    OutputStream output = response.getOutputStream();
    // Now you can write the InputStream of the file to the above OutputStream the usual way.
    // ...

    fc.responseComplete(); // Important! Otherwise JSF will attempt to render the response which obviously will fail since it's already written with a file and closed.
}

Ejemplo de archivo estático común

En caso de necesita transmitir un archivo estático desde el sistema de archivos de disco local, sustituya el código de la siguiente manera:

File file = new File("/path/to/file.ext");
String fileName = file.getName();
String contentType = ec.getMimeType(fileName); // JSF 1.x: ((ServletContext) ec.getContext()).getMimeType(fileName);
int contentLength = (int) file.length();

// ...

Files.copy(file.toPath(), output);

Ejemplo de archivo dinámico común

En caso de que necesite transmitir un archivo generado dinámicamente, como PDF o XLS, simplemente proporcione output allí donde la API que se utiliza espere un OutputStream.

Por ejemplo, iText PDF:

String fileName = "dynamic.pdf";
String contentType = "application/pdf";

// ...

Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, output);
document.open();
// Build PDF content here.
document.close();

Por ejemplo, Apache POI HSSF:

String fileName = "dynamic.xls";
String contentType = "application/vnd.ms-excel";

// ...

HSSFWorkbook workbook = new HSSFWorkbook();
// Build XLS content here.
workbook.write(output);
workbook.close();

Tenga en cuenta que no puede establecer la longitud del contenido aquí. Por lo tanto, debe eliminar la línea para establecer la respuesta longitud del contenido. Esto técnicamente no es un problema, la única desventaja es que el usuario final se presentará un progreso de descarga desconocido. En caso de que esto sea importante, entonces realmente necesita escribir en un archivo local (temporal) primero y luego proporcionarlo como se muestra en el capítulo anterior.

Método de utilidad

Si está utilizando JSF utility library OmniFaces , entonces puede usar uno de los tres convenientes Faces#sendFile() métodos que toman a File, o an InputStream, o a byte[], y especificando si el archivo debe descargarse como un archivo adjunto (true) o en línea (false).

public void download() throws IOException {
    Faces.sendFile(file, true);
}

Sí, este código está completo tal cual. No necesitas invocar responseComplete() y así sucesivamente. Este método también trata correctamente con encabezados específicos de IE y nombres de archivo UTF-8. Puede encontrar el código fuente aquí.

 207
Author: BalusC,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-09-26 13:08:44
public void download() throws IOException
{

    File file = new File("file.txt");

    FacesContext facesContext = FacesContext.getCurrentInstance();

    HttpServletResponse response = 
            (HttpServletResponse) facesContext.getExternalContext().getResponse();

    response.reset();
    response.setHeader("Content-Type", "application/octet-stream");
    response.setHeader("Content-Disposition", "attachment;filename=file.txt");

    OutputStream responseOutputStream = response.getOutputStream();

    InputStream fileInputStream = new FileInputStream(file);

    byte[] bytesBuffer = new byte[2048];
    int bytesRead;
    while ((bytesRead = fileInputStream.read(bytesBuffer)) > 0) 
    {
        responseOutputStream.write(bytesBuffer, 0, bytesRead);
    }

    responseOutputStream.flush();

    fileInputStream.close();
    responseOutputStream.close();

    facesContext.responseComplete();

}
 3
Author: John Mendes,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-10-03 10:01:56

Esto es lo que funcionó para mí:

public void downloadFile(String filename) throws IOException {
    final FacesContext fc = FacesContext.getCurrentInstance();
    final ExternalContext externalContext = fc.getExternalContext();

    final File file = new File(filename);

    externalContext.responseReset();
    externalContext.setResponseContentType(ContentType.APPLICATION_OCTET_STREAM.getMimeType());
    externalContext.setResponseContentLength(Long.valueOf(file.lastModified()).intValue());
    externalContext.setResponseHeader("Content-Disposition", "attachment;filename=" + file.getName());

    final HttpServletResponse response = (HttpServletResponse) externalContext.getResponse();

    FileInputStream input = new FileInputStream(file);
    byte[] buffer = new byte[1024];
    final ServletOutputStream out = response.getOutputStream();

    while ((input.read(buffer)) != -1) {
        out.write(buffer);
    }

    out.flush();
    fc.responseComplete();
}
 2
Author: Koray Tugay,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-04-28 09:06:43

Aquí está el fragmento de código completo http://bharatonjava.wordpress.com/2013/02/01/downloading-file-in-jsf-2 /

 @ManagedBean(name = "formBean")
 @SessionScoped
 public class FormBean implements Serializable
 {
   private static final long serialVersionUID = 1L;

   /**
    * Download file.
    */
   public void downloadFile() throws IOException
   {
      File file = new File("C:\\docs\\instructions.txt");
      InputStream fis = new FileInputStream(file);
      byte[] buf = new byte[1024];
      int offset = 0;
      int numRead = 0;
      while ((offset < buf.length) && ((numRead = fis.read(buf, offset, buf.length -offset)) >= 0)) 
      {
        offset += numRead;
      }
      fis.close();
      HttpServletResponse response =
         (HttpServletResponse) FacesContext.getCurrentInstance()
        .getExternalContext().getResponse();

     response.setContentType("application/octet-stream");
     response.setHeader("Content-Disposition", "attachment;filename=instructions.txt");
     response.getOutputStream().write(buf);
     response.getOutputStream().flush();
     response.getOutputStream().close();
     FacesContext.getCurrentInstance().responseComplete();
   }
 }

Puede cambiar la lógica de lectura del archivo en caso de que desee que el archivo se genere en tiempo de ejecución.

 -2
Author: Bharat Sharma,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-02-05 10:57:14