【问题标题】:File download not working in Play Framework app deployed in Heroku文件下载在 Heroku 中部署的 Play Framework 应用程序中不起作用
【发布时间】:2016-10-27 05:35:12
【问题描述】:

我在 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=

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

【问题讨论】:

  • 我唯一的猜测是尝试使用 Content-TypeContent-Length 等请求标头
  • 与您的本地文件系统相比,Heroku filesystem is ephemeral 可能暗示正在发生的事情。另一个可能的原因是您没有(在 heroku dyno 上)写入文件的权限。我建议您在编写文件时更改代码以记录异常,并获取有关错误原因的更多信息。
  • 从 Play 2.3 升级到 2.5 后我遇到了同样的问题。你解决过这个问题吗?

标签: scala heroku playframework


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2015-11-13
    • 1970-01-01
    • 2021-10-28
    • 1970-01-01
    • 2023-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多