1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
| import javax.faces.context.FacesContext;
import javax.servlet.http.*;
import javax.servlet.ServletOutputStream;
import java.io.IOException;
import java.io.*;
/**
*
* @author
*/
public class DownloadBean {
/** Creates a new instance of DownloadBean */
public DownloadBean() {
}
public void sendFile(String mimeType) {
FacesContext faces = FacesContext.getCurrentInstance();
HttpServletResponse response = (HttpServletResponse) faces.getExternalContext().getResponse();
HttpServletRequest request = (HttpServletRequest) faces.getExternalContext().getRequest();
String filename = "myFile.txt" //"/home/repertoire/monFichierDAide.hlp" pour mon fichier d'aide;
byte[] data = getSomeData(request);
// Note that different browsers will behave differently and you have no control over this.
// We'll use different mime types to implement the different "Open" and "Save" behaviors.
// "application/x-unknown" will be the MIME type used for the "Save" behavior.
response.setContentType(mimeType);
response.setContentLength(data.length);
// Cross-browser hack for Firefox 1.0.7 and IE 6 compatibility.
// IE 6 ignores the MIME type and decides based on the "attachment" or "inline"
if (mimeType.equals("application/x-unknown")) {
// Show the "Save As..." dialog
response.setHeader( "Content-disposition", "attachment; filename=\"" + filename + "\"");
} else {
// attempt to "open" the file
response.setHeader( "Content-disposition", "inline; filename=\"" + filename + "\"");
}
// Now we start sending data with the response object.
try {
ServletOutputStream out;
out = response.getOutputStream();
out.write(data);
} catch (IOException e) {
e.printStackTrace();
}
faces.responseComplete();
}
public byte[] getSomeData(Object o) {
// ignore the HttpServletRequest object and return some data.
return "Hello World!".getBytes();
}
} |
Partager