【问题标题】:How can I download a file which I have dynamically created on Apache TomCat Server?如何下载我在 Apache TomCat 服务器上动态创建的文件?
【发布时间】:2023-04-07 03:53:01
【问题描述】:

我使用“Apache POI”创建了一个“.xlsx”文件 (CustomerData.xlsx)。

问题是文件是在我的 TomCat 服务器上创建的,我必须下载它。

我尝试了以下代码,以下载文件:

HttpServletResponse response = null;

response.setContentType("xlsx");
response.setHeader(
    "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
    "attachment; filename=C:\CustomerData.xlsx");
try {
    workbook.write(response.getOutputStream());
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

但不幸的是,它似乎不起作用。

如果您有任何想法或建议,请不要犹豫。

【问题讨论】:

标签: java tomcat jakarta-ee primefaces apache-poi


【解决方案1】:

您将内容类型和附件信息混合到一个无法正常工作的标头中。

改为写

response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setHeader("Content-Disposition", "attachment;filename=CustomerData.xlsx");

【讨论】:

  • 感谢您的回答。我相应地应用了更改,但不幸的是它不起作用。
  • @StavrosVrakas “不起作用”确实没有太多信息。
猜你喜欢
  • 2015-06-19
  • 1970-01-01
  • 2018-04-26
  • 2011-12-05
  • 2016-08-29
  • 2015-07-02
  • 1970-01-01
  • 2018-02-13
  • 1970-01-01
相关资源
最近更新 更多