JSF生命周期问题,responseComplete()无法正常跳出

huahuashijie888 2008-12-05
在jsf页面的文件打开组件
<h:outputText  value="原文:"/>
<h:commandLink title="#{Taskprogress.originaltext}" styleClass="uploadFile" action="#{Taskprogress.openOriginalFile}">
<h:outputText id="originaltext" value="#{Taskprogress.originaltext}"> </h:outputText>

</h:commandLink>
当我点击链接的时候,跳出文件对话框,执行保存,打开,或者取消操作后,我再保存当面页面,但是又会跳出文件下载对话框,action源码如下:
public static void openFile(String fileName,String subDir)
{
try
{
HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
if(FacesContext.getCurrentInstance().getResponseComplete())return;
if (!subDir.equals("")) subDir = subDir + "\\";
fileName = request.getRealPath("upload")+"\\" + subDir + fileName;
java.io.File f = new java.io.File(fileName);
if (f.exists()&&f.canRead())
{

response.setContentType ("application/x-msdownload octet-stream;charset=ISO8859-1");
response.setHeader ("Content-Disposition", "attachment;filename=\"" + new String(f.getName().getBytes(), "ISO8859-1") + "\"");
// response.setContentLength((int) f.length());
java.io.BufferedInputStream fileInputStream = new java.io.BufferedInputStream(new java.io.FileInputStream(f));

int i;
// java.io.OutputStream out = response.getOutputStream();
java.io.PrintWriter out = response.getWriter();
// out.clearBuffer();
while ((i=fileInputStream.read()) != -1) out.write(i);
fileInputStream.close();
out.flush();
out.close();
FacesContext.getCurrentInstance().responseComplete();
}

} catch (IOException e) {
e.printStackTrace();
}

// String fileType = fileName.substring(fileName.lastIndexOf(".")+1);
// if (fileType.equals("doc"))
// openWordFile(fileName);
// else if  (fileType.equals("xls"))
// openExcelFile(fileName);
}
为什么不能结束本次请求?想半天了,麻烦指点指点!
Global site tag (gtag.js) - Google Analytics