如何使用 Scalatra 将文件发送到浏览器?
我正在使用 scalatra-sbt-prototype。为了能够从我的文件系统上的目录提供文件,我需要修改什么以及在哪里?举例来说,我想从 /home/downloads/first.tar.gz
提供文件 first.tar.gz
,并通过以下方式访问它:
http://localhost:8080/first.tar.gz
I'm using the scalatra-sbt-prototype. What would I have to modify, and where , to be able to serve files from a directory on my filesystem? Say for example , I would want to serve the file first.tar.gz
from /home/downloads/first.tar.gz
, and have it accessible as:
http://localhost:8080/first.tar.gz
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为了正确性,您可能还需要设置 contentType,以便浏览器不会尝试将其显示为 text/html(如果您在 before 过滤器中设置了该内容并且具有正确名称的处置标头)。大多数浏览器会从 url 推断文件名,但为了确定,您可以显式设置它。
显然,该路线非常静态,但会执行您想要的操作。
For correctness, you may also want to set the contentType so the browser doesn't try to display it as text/html if you have that set in your before filter and the disposition header with the correct name. Most browsers will infer the filename from the url but just to be sure, you can set it explicitly.
Obviously the route is very static but will do what you want.
从一个操作返回一个
java.io.File
到浏览器,似乎可以完成这个任务。Returning a
java.io.File
to the browser, from an action, seems to accomplish this.