斯卡拉喷雾。将文本输出为文件

时间:2015-12-10 09:50:24

标签: scala file http spray

我在DB中有一些文本数据,我希望按文件将这些数据输出给用户。我怎样才能做到这一点?现在我这样做:1。创建tmp文件。 2.将数据写入文件。 3.使用getFromFile()spray方法提供此文件。 没有1步和2步就可以做到这一点吗?

1 个答案:

答案 0 :(得分:0)

您可以执行以下操作:

respondWithMediaType("text/plain") {
      respondWithHeader(HttpHeaders.`Content-Disposition`.apply("attachment", Map("filename" -> ("text.txt")))) {
        implicit val bufferMarshaller = BasicMarshallers.byteArrayMarshaller("text/plain")
        autoChunk(ChunkSizeMagnet.fromChunkSize(5000000)) {
          withRangeSupport() {
            complete("file content".getBytes)
          }
        }
      }
    }