File Show not Working
jsfgeeks Jan 28, 2010 3:39 AMhi,
In my project using link to show a file from resources,but it's not working wirh richfaces it shows balnk page. in my pure Jsf project it was working properly.
My Code is below:
<h:commandLink id="file2_link" binding="#{newsEvents.file2_link}"
action="#{newsEvents.file2_link_action}" title="click to view file" />
public String file2_link_action() {
// TODO: Replace with your code
try {
if (file2_link.getValue() != null) {
String path1 = sc.getRealPath(ex_file_path) +"/"+file2_link.getValue().toString(); //sc=ServetContext
System.out.println(path1);
String extension1 = path1.substring(path1.lastIndexOf(".") + 1);
System.out.println(extension1);
File file = new File(path1);
BufferedInputStream input1 = null;
BufferedOutputStream output1 = null;
try {
// Open file.
input1 = new BufferedInputStream(new FileInputStream(file), DEFAULT_BUFFER_SIZE);
// Init servlet response.
response.reset();
response.setContentType("application/" + extension1 + "");
response.setContentLength(input1.available());
response.setHeader("Content-disposition", "inline; filename=\"" + path1 + "\"");
output1 = new BufferedOutputStream(response.getOutputStream(), DEFAULT_BUFFER_SIZE);
// Write file contents to response.
byte[] buffer1 = new byte[DEFAULT_BUFFER_SIZE];
for (int length1;
(length1 = input1.read(buffer1)) != -1;) {
output1.write(buffer1, 0, length1);
} // Finalize task.
output1.flush();
} finally {
// Gently close streams.
input1.close();
output1.close();
}
}
} catch (Exception e) {
System.out.println(e);
}
FacesContext.getCurrentInstance().responseComplete();
return null;
}