Play production mode java.io.FileNotFoundException that thing.

Source: Internet
Author: User

Before the article "using Scala+play to build a geographic data query interface," The data used is a JSON-formatted text file Area.json, stored in the Conf/jsons folder. At first, it is placed under the public/folder, which runs in the online quasi-production mode:

Activator Dist

Get Mosquito-1.0.zip compressed package, after decompression:

Go to the/bin directory to run the mosquito pin this newspaper wrong:

Java.io.FileNotFoundException

Then go to unzip the mosquito-1.0/see found and there is no public folder, this shows public folder in Dist packaging is compiled merged into other folders (as for the specific after compiling, need to look at the source code). But also found conf/folder still exist, so the solution is also simple, will Area.json put to Conf/jsons, Aberdeen Re:

Package (Activator Dist) –> decompression –> run (mosquito)

Everything is fine.

I thought that this thing had been completely solved, and then the egg.

It then wrote "Upload and download with play (super easy!)", which was run directly in the local Windows environment Development mode without discovering the problem. After that, the change was made: The image is displayed directly on the page after uploading the image. This requirement is used when posting an article, inserting a picture into the article and previewing it. Then this time, you need to upload and return the file address, through routes:

GET         /assets/*file            controllers. assets.at (path= "/public", file)

To access, at this time the uploaded folder under Public/upload, at this time in the development mode is also completely no problem.

But it is not reasonable to think of uploading files under the public/, if the public/under the Css,image and other static files more or upload more than the word is also more chaotic, moreover, put into the public/under the production mode can not run successfully. So in the project root of the new Res folder, modify the upload file location, at this time the file can be uploaded to this, but there is a new problem: The returned file address through "Get/assets/*file" can not be obtained, HTTP code 404.

By reviewing the official documentation, you know:

However, if you define Assets.at -mappings for the action, like this:

GET  /javascripts/*file        controllers. assets.at (path= "/public/javascripts", file) GET  /images/*file             controllers. assets.at (path= "/public/images", file)

You'll then need-specify both parameters when using the reverse router:

<src= "@routes. assets.at ("/public/javascripts", "Jquery.js") "></script>  <src= "@routes. assets.at ("/public/images", "Logo.png") ">

Then instead:

GET  /javascripts/*file        controllers. assets.at (path= "/public", file) GET  /res/file             controllers. assets.at (path= "/res", file)

At this point you will:

<rel= "stylesheet"  media= "screen"  href= "@routes . assets.at ("stylesheets/main.css") ">

Switch

<rel= "stylesheet"  media= "screen"  href = "@routes. assets.at ("public/stylesheets/main.css") ">

In this code, "@routes. assets.at. "is a reverse route, the routes is configured with 2 this, so you need to give a relatively accurate path, otherwise the reverse route will not match successfully.

This time to run, or can not access through the Get/res res/files, do not know why.

Find the following workaround in stackoverflow.com:

Create Resourcectrl under the Controllers package and join:

Object Resourcectrl extends controller{    val absolutepath = "" ^ (/|[ a-za-z]:\\). * "" ". R    def at (rootpath:string, file:string): action[anycontent] = Action {request =        val Fileur Ldecode = Java.net.URLDecoder.decode (file, "Utf-8");   Chinese need urldecoder        val filetoserve = rootpath Match {case                     Absolutepath (_) = = New File (RootPath, Fileurldecode) Case            _ = = new File (Play.application.getFile (RootPath), Fileurldecode)        }        if (filetoserve.exists) {            ok.sendfile (filetoserve, inline = true)        } else {            logger.error ("Photos Controller failed to serve Photo: "+ Fileurldecode) NotFound}}}    

Change the previous routes in Get/res to:

GET         /resource/*file          controllers.resourceCtrl.at (path= "/", file)

Run, all OK, the problem is solved (in development mode).

Then run in production mode, get the uploaded file via routes get/res, error:

Java.io.filenotfoundexception:d:\mosquito-project\mosquito\target\universal\mosquito-1.0\mosquito-1.0\bin\res ( The system could not find the path specified. )

After a long day, and back to the original point.

But notice that the error message, is: Mosquito-1.0\bin, this address is obtained by Play.application.getFile. What does that mean?

This means that the path obtained through Play.application (Play.current.path) is directly under the Bin/folder in the project root directory, not the root directory of the project.

The correct address should be:

D:\mosquito-project\mosquito\target\universal\mosquito-1.0\mosquito-1.0\res

"Access Denied" appears when I replace play.application with the correct address.

But me. The same program is put on Linux running, everything is normal, everything is normal, everything is normal! The path is also the root directory correctly when you print Play.current.path.

And, at the same time, I have noticed:

The logs folder generated by the production mode run on Windows under bin/

Production mode run on Linux generates the logs folder under the root directory

I do not yet know why, and then research.

Play production mode java.io.FileNotFoundException that thing.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.