首页 > 解决方案 > 调用servlet进行文件下载时,JSF命令按钮只能点击一次

问题描述

我在 jsf 中有一些命令按钮,其中一个在创建文件和下载时单击。在处理 jsf action 的 action 类中,我创建了 url 对象,该对象具有用于调用 Servlet 的 URL。这一切正常,当我单击按钮时会下载一次文件,但问题是,此后我无法单击页面上的按钮或任何其他命令按钮。为什么请求不完整?请帮忙。

<h:commandButton id="filedownloadbtn" action="#{fileDownloadInit.submit}" value = "thisform">

行动

 try {

String             baseURL =  facesContext.getCurrentInstance().getExternalContext().getRequestContextPath();
          String             url            = baseURL + "/DataloadServlet";


            facesContext.getCurrentInstance().getExternalContext().redirect(url);
            return null;
        } finally {

            facesContext.responseComplete();
        }'

数据加载服务程序

 public Object[] getFileNameAndData(HttpServletRequest request)
    {   

            ByteArrayOutputStream stream = new ByteArrayOutputStream();

            //does some processing...

            return new Object[] {fileName, stream.toByteArray()};
}

文件下载servlet

public abstract class  FileDownloadservlet extends javax.servlet.http.HttpServlet
{
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        Object[] file = getFileNameAndData(request);
            if (file != null)
            {
                String fileName = (String)file[0];
                byte[] fileData = (byte[])file[1];

                response.setHeader("Content-Disposition", "attachment;filename=\"" + fileName +"\"");



                    response.setHeader("Cache-Control", "no-cache, no-store"); // HTTP 1.1.
                    response.setHeader("Pragma", "no-cache"); // HTTP 1.0.


                String contentType = "application/vnd.ms-excel";
                response.setContentType(");
                response.setContentLength(fileData.length);
                try
                {
                    OutputStream output = response.getOutputStream();
                    output.write(fileData);
                    output.flush();
                    output.close();
                }
                catch (IOException ex)
                {

                }
            }

}

标签: javajspjsfservletsjsf-1.2

解决方案


推荐阅读