[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:
See also: Faces#getContext() | Utils#stream() | Faces#getMimeType() | Utils#encodeURL()
Method for sending a file to the response (download) as a byte[]:
Method for sending a file to the response (download) as a InputStream:
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;
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);
}
Or, if we
are using more OmniFaces utilities, we can write this (follow Faces#getResourceAsStream()):
import org.omnifaces.util.Faces;
import java.io.InputStream;
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