无法在 Play for Scala 中发送到浏览器 ByteArrayOutputStream

Cannot send to the browser ByteArrayOutputStream in Play for Scala

我在 Play for Scala 中有以下代码试图将 Excel 文件发送到浏览器:

            val out = new ByteArrayOutputStream
            val exporter = new org.pivot4j.ui.poi.ExcelExporter(out)
            val renderer = new TableRenderer
            renderer.render(model, exporter);
            out.flush
            out.close
            Ok(out).withHeaders(
                  CONTENT_TYPE -> "application/vnd.ms-excel",
                  CONTENT_DISPOSITION -> s"attachment; filename = file.xlsx")

Ok 抛出一个编译错误:

◾Cannot write an instance of java.io.ByteArrayOutputStream to HTTP response. Try to define a Writeable[java.io.ByteArrayOutputStream]

虽然ByteArrayOutputStream是输出类型,但是不能写入。如何解决这个问题?

尝试在 out 上调用 toByteArray,如下所示:

Ok(out.toByteArray).withHeaders(...)

DefaultWritables provides a list of Writable provided by Play out-of-the-box. In particular, there exists Writeable[Array[Byte]] 应该在您致电 toByteArray

时接听