【发布时间】:2017-07-25 14:56:27
【问题描述】:
我正在将使用 Apache POI 创建的 Excel 工作簿直接写入响应对象,如下所示,而不创建文件:
val outputStream: ByteArrayOutputStream = new ByteArrayOutputStream()
workbook.write(outputStream)
ExcelOk(response.getOutputStream.write(outputStream.toByteArray))
但是一旦响应的大小超过 8kB,它就会开始在 Chrome 中下载为 zip 文件,在 FireFox 中下载为 octet-stream。
我的ExcelOk 对象如下所示:
object ExcelOk {
def apply(body: Any = Unit, headers: Map[String, String] = ExcelContentType, reason: String = "") = {
halt(ActionResult(responseStatus(200, reason), body, headers ))
}
}
我的ExcelContentType(即响应头)如下:
val ExcelContentType = Map(
"Access-Control-Allow-Credentials" -> "true",
"Access-Control-Allow-Methods" -> "GET, PUT, POST, DELETE, OPTIONS",
"Access-Control-Allow-Origin" -> "*",
"Access-Control-Max-Age" -> "1728000",
"Content-type" -> "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
"Content-disposition" -> "attachment; filename=excel_report.xlsx"
)
我什至尝试将"Transfer-Encoding" -> "chunked" 添加到标题列表中,但它不起作用。
我也在我的web.xml 文件中添加了这个 sn-p,但它也没有帮助:
<mime-mapping>
<extension>xlsx</extension>
<mime-type>application/vnd.openxmlformats-officedocument.spreadsheetml.sheet</mime-type>
</mime-mapping>
任何有关这方面的帮助都会很有用。请注意,只有在响应大小超过某个阈值后才会观察到此行为。
【问题讨论】: