Hi there,
I am writing JSF portlet using myFaces. The JSF portlet will download a file
to local from server and the file in the server side will be deleted after
downloaded. The code works fine as a web application. But it can not work
properly as portlet.
In a jsp page, a button is displayed to invoke the downloadFile method.
<h:commandButton value="downLoad" action="#{myFile.downloadFile}" />. When
running my JSF code as web application, to click the "downLoad" button, a
window will popup to display "open" or "save" the file. But if running as
JSF portlet, when click the "downLoad" button, no window pops up. But
the file in the server side does be deleted after the button is clicked.
any idea?
Thanks a lot in advance.
Below is the method to download file from server:
public void downloadFile(){
javax.faces.context.FacesContext conText =
javax.faces.context.FacesContext.getCurrentInstance();
javax.servlet.http.HttpServletResponse response = (
javax.servlet.http.HttpServletResponse
)conText.getExternalContext().getResponse();
String path = System.getProperty("java.io.tmpdir");
filename = "test.ppt";
File file = null;
try {
String _fileString = path + File.separator + filename;
file = new File(_fileString);
java.io.FileInputStream fis = new java.io.FileInputStream(file);
String contenttype = "application/octet-stream";
response.setContentType(contenttype);
response.setHeader("Content-Disposition", "attachment;
filename=" + filename + ";");
javax.servlet.ServletOutputStream out = response.getOutputStream
();
int c = 0;
while ((c=fis.read())!=-1) {
out.write(c);
}
out.flush();
out.close();
file.delete();
} catch (Exception e) {
if (file!=null && file.exists()) {
file.delete();
System.out.println("[srb] file deleted on web server");
}
return;
}
conText.getApplication
().getStateManager().saveSerializedView(conText);
conText.responseComplete();
}