Seam默认下载文件会变成直接在网页中打开内容,主要是因为Response header的问题,需要手工调节下:
@Name("outExcelDown")public class OutExcelDown {
@In(value="#{facesContext.externalContext}")
private ExternalContext extCtx;
@In(value="#{facesContext}")
FacesContext facesContext;
public void downfile(){
HttpServletRequest request =(HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
String path=request.getSession().getServletContext().getRealPath("/") +“123.xls”;
response.setContentType("application/vnd.ms-excel ");
response.addHeader("Content-disposition", "attachment; filename=456.xls");
try {
InputStream ips= new FileInputStream("path");
OutputStream ops = response.getOutputStream();
int data = -1;
while((data = ips.read()) != -1) {
ops.write(data);
}
ops.close();
ops.flush();
facesContext.responseComplete();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}