文件下载无法在Heroku中部署的Play Framework应用程序中运行

时间:2016-10-27 05:35:12

标签: scala heroku playframework

我在Play应用中有一个按钮可以下载文件,这是一个简单的Action响应:

def exportData(query: String): Action[AnyContent] = Action{ implicit request =>

    val file = new java.io.File("test.txt")
    val writer = new PrintWriter(file) //prepare the file + writer
    val data = SearchLib.get(query,request) //get data for file
    for(result <- data){ 
      //process looping through data, writing the lines to file
    }
    writer.close()

    Ok.sendFile( //send the file back to the user
      content = file,
      fileName = _ => query + "_search.txt",
      inline = false
    )
}

它在本地主机上运行得很好,但是当我将它部署到Heroku并尝试它时,我得到一个代码为H18的应用程序错误(服务器套接字错误)

2016-10-27T05:17:20.828591+00:00 app[web.1]: GET /export/nitrate
2016-10-27T05:17:21.383364+00:00 heroku[router]: sock=backend at=error code=H18 desc="Server Request Interrupted" method=GET path="/export/nitrate" host=pathway-query.herokuapp.com request_id=85a13382-5d50-4f8e-98d5-f60198083d9a fwd="174.6.50.44" dyno=web.1 connect=1ms service=506ms status=503 bytes=

应用中的任何其他类型的请求或功能都可以正常工作。任何想法为什么会这样?

1 个答案:

答案 0 :(得分:1)

如果其他人正在阅读本文,我通过使用分块响应而不是提供整个文件来解决此问题-https://www.playframework.com/documentation/2.5.x/JavaStream#Chunked-responses通过将文件包装到FileInputStream