Scala 如何在akka http中完成响应后进行清理?
我生成一个文件作为响应发送。响应下载后,您希望删除该文件.csvScala 如何在akka http中完成响应后进行清理?,scala,file,http,akka,Scala,File,Http,Akka,我生成一个文件作为响应发送。响应下载后,您希望删除该文件.csv path("hello") { get { complete(HttpEntity.fromFile(ContentTypes.`text/csv(UTF-8)`,file.csv)) } } 这就是解决办法 val source = FileIO.fromPath(file.toPath) .watchTermination() { case (
path("hello") {
get {
complete(HttpEntity.fromFile(ContentTypes.`text/csv(UTF-8)`,file.csv))
}
}
这就是解决办法
val source = FileIO.fromPath(file.toPath)
.watchTermination() { case (_, result) =>
result.onComplete(_ => {
file.delete()
log.info(s"file Deleted ${file.getPath}")
}
)
}
complete(HttpEntity(ContentTypes.`text/csv(UTF-8)`, source))
这对我来说很好