简体   繁体   English

在play框架中读取上传文件

[英]read upload file in play framework

I'm using play framework 2.1.0, upload files to play-app/upload folder. 我正在使用播放框架2.1.0,将文件上传到play-app/upload文件夹。

then I run play 'start -Dhttp.port=80' to start server. 然后我运行play 'start -Dhttp.port=80'来启动服务器。

but when I upload a file to play-app/upload folder, it can not be access immediately. 但是当我将文件上传到play-app/upload文件夹时,无法立即访问它。

if I stop the server and start again, then I can access the file. 如果我停止服务器并重新启动,则可以访问该文件。

How can I solve this problem? 我怎么解决这个问题? Thanks a lot. 非常感谢。

ps, I route /upload as below: ps,我将/upload路由如下:

GET     /upload/*file               controllers.Assets.at(path="/upload", file)

Could it be that static files are loaded once? 可能是静态文件一次加载了吗? How can I solve it? 我该如何解决?

最好制定上传文件夹中的应用程序的文件夹之外,并添加它就像路径/home/navins/upload-folder/application.conf ,那么你就可以访问它的全部时间,你也可以在那里上传文件不仅适用于应用程序(即通过FTP),无需重启。

I think what you need is to define a sort of Remote assets controller. 我认为您需要定义一种Remote assets控制器。 Basically, once a file is uploaded, you put it in a folder that is outside your application's folder. 基本上,一旦上传了文件,就将其放在应用程序文件夹之外的文件夹中。 Then, use a controller that will let you access it. 然后,使用将允许您访问它的控制器。 Here is an example: http://www.jamesward.com/2012/08/08/edge-caching-with-play2-heroku-cloudfront 这是一个示例: http : //www.jamesward.com/2012/08/08/edge-caching-with-play2-heroku-cloudfront

Here, James Ward creates a controller to access assets that are stored on cloudfront, what you need to do is to write a similar controller and replace the "content url" with the absolute path to your "Uploaded files directory". 在这里,James Ward创建了一个控制器来访问存储在cloudfront上的资产,您需要做的是编写一个类似的控制器,并将“内容url”替换为“已上传文件目录”的绝对路径。

finally what I've solve it by adding access method in controller: 最后,我通过在控制器中添加访问方法解决了它:

public static Result view(String filename) {
    File file  = new File(Play.application().path().getAbsolutePath() + "/upload/" + filename);
    return ok(file);
}

then, change route conf, you can access the files by the method. 然后,更改route conf,您可以通过方法访问文件。

BTW, if you are using play framework below 2.0, you may user: 顺便说一句,如果您正在使用2.0以下的播放框架,则可以使用:

    renderBinary(file, ContentType);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM