是否有一种默认方法可以读取播放中的文件,即使在生产环境中也能正常工作? [英] Is there a default method to read a file in play which works even in the production environment?

查看:41
本文介绍了是否有一种默认方法可以读取播放中的文件,即使在生产环境中也能正常工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有 java 的 play 框架来开发 Web 应用程序.我想在运行时读取一个文件,并且该文件已包含在项目的文件夹中.但是在生产环境中,该文件夹不可用.有没有办法让文件夹在运行时可用?特别是,是否有一种默认方法可以处理这种情况?

I am using play framework with java to develop a web application. I have wanted to read a file at the run time and the file has been included in a folder in the project. But at the production environment the folder is not available. Is there a way to make the folder available at the run time? And specially, is there a default method in play to handle such a scenario?

我回答了如何在 Play Framework 2.2.1 中读取文件?1" 但是在生产环境中没有提到这个问题.

I went through the question "How to read a file in Play Framework 2.2.1?1" But there is no mention about the problem in the production environment.

推荐答案

对于某些特定任务,您可以选择您喜欢的位置,而不是在专用文件夹中.例如,您可以创建/resources 文件夹.不要在/app 文件夹中创建任何资源文件夹,因为它只是一个存储代码的位置.其他特定文件夹也是如此.然后你可以使用

You can choose a location you prefer other than in dedicated folders for some specific tasks. For an example you can create /resources folder. Don't make any resource folder inside /app folder since it is only a location to store code. Same goes with other specific folders. Then you can use

import play.Play; 
    Play.application().getFile("relative_path_from_<Project_Root>);

访问代码中的文件.

只有这样,才能在开发环境中完美运行.但是,一旦您使用 dist 文件将其投入生产,它将无法工作,因为您放置的整个资源文件夹都不会添加到 dist 中.为了做到这一点,你必须明确要求 play 将/resources 文件夹也添加到你的 dist 中.为此,您需要做的是转到您的/build.sbt 并添加这些行

Only this, will work perfectly on dev environment. But once you put this in production using the dist file, it will not work since the entire resources folder you put will not be added to the dist. In order to do that, you have to explicitly ask play to add the /resources folder to your dist also. For that what you have to do is go to your /build.sbt and add these lines

import com.typesafe.sbt.packager.MappingsHelper._
    mappings in Universal ++= directory(baseDirectory.value / "resources")

现在,如果你检查你的 dist,你会看到它在 dist 中有一个额外的文件夹resources".然后它也适用于生产环境.

Now if you take and check your dist, you can see it has an additional folder 'resources' inside the dist. Then it will work for the production environment also.

这篇关于是否有一种默认方法可以读取播放中的文件,即使在生产环境中也能正常工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆