Xaio,
Yes, this won't work in a portal environment. Your "file" needs to be a
resource that you retrieve and, as such, the url to your file needs to
be encoded as a resource url. Nuisances aside, consider a "normal"
portal usecase where an "action" will cause all portlets on the screen
to resubmit. This means that the Portal needs to return all of it's
data, including data for itself and all the other portlets, to the
screen at the same time. The Portal, therefore, has to force the
content type to be something compatible with itself and all portlets.
My suggestion would be to create a servlet which deletes the file for
you and sends the resource back with the correct content type. You
would then encode the url to this servlet as a resource url and use
something like a goLink to access it.
Also note that some renderkits will do the encoding for you, so you
should make sure that you use the correct component to reference this
servlet. I know the Trinidad goLink encodes url's as a resource url.
I'm not sure about the other renderkits.
Scott
xiao wang wrote:
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();
}