java - 调用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)
{
}
}
}
解决方案
推荐阅读
- python-3.x - 无法在 chromebook 上打开 anaconda-navigator
- java - 构建自定义过滤器以每个标识符在 M 个时间单位内处理 N 个请求
- ios - iOS CallKit CXCallObserver 委托方法 callChanged 永远不会触发
- python - 文件对象迭代器是否“损坏”?
- spring - 优化 Spring Boot REST 控制器/冗余请求处理程序查找
- c++ - 检查类型是否为 std::basic_string
在 C++ 编译时 - c# - Autofac.ILifetimeScope:无法模拟或解析我的服务类
- python - 在 python 脚本中安装没有 pip 的模块
- c# - .NET 模拟不起作用
- wordpress - 使用 Wordpress 页面代码从 Pardot 登录页面中删除不需要的符号