Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
212 views
in Technique[技术] by (71.8m points)

java - Is there a default method to read a file in play which works even in the production environment?

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?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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>);

to access the file inside your code.

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")

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...