My JSF Books/Videos My JSF Tutorials OmniFaces/JSF PPTs
JSF 2.3 Tutorial
JSF Caching Tutorial
JSF Navigation Tutorial
JSF Scopes Tutorial
JSF Page Author Beginner's Guide
OmniFaces 2.3 Tutorial Examples
OmniFaces 2.2 Tutorial Examples
JSF Events Tutorial
OmniFaces Callbacks Usages
JSF State Tutorial
JSF and Design Patterns
JSF 2.3 New Features (2.3-m04)
Introduction to OmniFaces
25+ Reasons to use OmniFaces in JSF
OmniFaces Validators
OmniFaces Converters
JSF Design Patterns
Mastering OmniFaces
Reusable and less-verbose JSF code

My JSF Resources ...

Java EE Guardian
Member of JCG Program
Member MVB DZone
Blog curated on ZEEF
OmniFaces is an utility library for JSF, including PrimeFaces, RichFaces, ICEfaces ...

.

.

.

.

.

.

.

.


[OmniFaces Utilities] - Find the right JSF OmniFaces 2 utilities methods/functions

Search on blog

Petition by Java EE Guardians

Twitter

duminică, 19 aprilie 2015

[OmniFaces utilities (2.2/2.0)] Send the given file to the response (download) as File / byte[] or InputStream


[OmniFaces utilities] The sendFile(File file, boolean attachment) method send the given file to the response. The content type will be determined based on file name. The content length will be set to the length of the file. The FacesContext#responseComplete() will implicitly be called after successful streaming. Give to this method the file to be sent to the response and indicate whether the file should be provided as attachment, or just inline via attachment argument.

[OmniFaces utilities] The sendFile(byte[] content, String filename, boolean attachment) method send the given byte array as a file to the response. The content type will be determined based on file name. The content length will be set to the length of the byte array. The FacesContext#responseComplete() will implicitly be called after successful streaming. Give to this method the file content as byte array, the filename which should appear in content disposition header and indicate whether the file should be provided as attachment, or just inline via attachment argument.

[OmniFaces utilities] The sendFile(InputStream content, String filename, boolean attachment) method send the given input stream as a file to the response. The content type will be determined based on file name. The content length may not be set because that's not predictable based on input stream. The client may receive a download of an unknown length and thus the download progress may be unknown to the client. Only if the input stream is smaller than the default buffer size, then the content length will be set. The InputStream#close() will implicitly be called after streaming, regardless of whether an exception is been thrown or not. The FacesContext#responseComplete() will implicitly be called after successful streaming. Give to this method the file content as input stream, the filename which should appear in content disposition header and indicate whether the file should be provided as attachment, or just inline via attachment argument.

Note The caller should preferably not catch the IOException thrown by this method,but just re-declare it in the action method.The Servlet container will handle it.

Note Starting with OmniFaces 2.2 this method was improved so no need to attempt setting content length if response is committed.

Method for sending a file to the response (download) as a File:

Method for sending a file to the response (download) as a byte[]:

Method for sending a file to the response (download) as a InputStream:
Usage:

The relative path of the file to download is: /resources/default/images/rafa.jpg

Download a file by indicating the java.io.File to be sent to the response (of course, the File may come from another place, this is just a test case):

import org.omnifaces.util.Faces;
...
public void downloadAction() throws IOException {
  FacesContext facesContext = FacesContext.getCurrentInstance();
  ExternalContext externalContext = facesContext.getExternalContext();
  Path path = Paths.get(externalContext.getRealPath("/resources/default/images/rafa.jpg"));
  Faces.sendFile(path.toFile(), true);
}

Or, if we are using more OmniFaces utilities, we can write this (follow Faces#getRealPath()):

import org.omnifaces.util.Faces;
import java.io.File;
...
public void downloadAction() throws IOException {
  Faces.sendFile(new File(Faces.getRealPath("/resources/default/images/rafa.jpg")), true);
}

Download a file by indicating the byte[] to be sent to the response (of course, the byte[] may come from another place, this is just a test case):

import org.omnifaces.util.Faces;
...
public void downloadAction() throws IOException {
  FacesContext facesContext = FacesContext.getCurrentInstance();
  ExternalContext externalContext = facesContext.getExternalContext();
  Path path = Paths.get(externalContext.getRealPath("/resources/default/images/rafa.jpg"));
  byte[] data = Files.readAllBytes(path);
  Faces.sendFile(data, "rafaelnadal.jpg", true);
}

Or, if we are using more OmniFaces utilities, we can write this (follow Faces#getRealPath()):

import org.omnifaces.util.Faces;
...
public void downloadAction() throws IOException {
  byte[] data = Files.readAllBytes(Paths.get(Faces.getRealPath("/resources/default/images/rafa.jpg")));
  Faces.sendFile(data, "rafaelnadal.jpg", true);
}

Download a file by indicating the InputStream to be sent to the response (of course, the InputStream may come from another place, this is just a test case):

import org.omnifaces.util.Faces;
import java.io.InputStream;
...
public void downloadAction() throws IOException {
  FacesContext facesContext = FacesContext.getCurrentInstance();
  ExternalContext externalContext = facesContext.getExternalContext();
  Path path = Paths.get(externalContext.getRealPath("/resources/default/images/rafa.jpg"));
  InputStream is = Files.newInputStream(path);
  Faces.sendFile(is, "rafaelnadal.jpg", true);
}

Or, if we are using more OmniFaces utilities, we can write this (follow Faces#getResourceAsStream()):

import org.omnifaces.util.Faces;
import java.io.InputStream;
...
public void downloadAction() throws IOException {      
  InputStream is = Faces.getResourceAsStream("/resources/default/images/rafa.jpg");
  Faces.sendFile(is, "rafaelnadal.jpg", true);
}

Niciun comentariu :

Trimiteți un comentariu

JSF BOOKS COLLECTION

Postări populare

OmniFaces/JSF Fans

Visitors Starting 4 September 2015

Locations of Site Visitors