如何更改play 2.0.1中的上传文件目录?

And*_*odi 2 java playframework playframework-2.0

我试图在我的application.conf文件中指定attachments.path属性,但这没有任何影响.

在play 2.0.1的文档中,我没有找到任何解释如何更改上传文件目录的内容.

我错过了什么吗?

bie*_*ior 10

虽然没有这样的变量,但application.conf您可以轻松地添加它并在您的方法中使用.按照您的意愿调用,即:

新行application.conf:

myUploadPath = "/home/your-account/some/custom/upload/folder/"
Run Code Online (Sandbox Code Playgroud)

根据文档样本:

public static Result upload() {
    MultipartFormData body = request().body().asMultipartFormData();
    MultipartFormData.FilePart picture = body.getFile("picture");
    if (picture != null) {
        String fileName = picture.getFilename();
        String contentType = picture.getContentType();
        File file = picture.getFile();

        // added lines
        String myUploadPath = Play.application().configuration().getString("myUploadPath");
        file.renameTo(new File(myUploadPath, fileName));

        return ok("file saved as " + myUploadPath + fileName);
    } else {
        flash("error", "Missing file");
        return redirect(routes.Application.uploadform());
    }
}
Run Code Online (Sandbox Code Playgroud)

使用此方法,您甚至可以filename clash在重命名之前执行检查,以防止随机覆盖.

  • 感谢您的回复,Marcus,但这并没有解决我的问题.我必须控制<示例>中的图片所在的文件夹.但是,我通过使用target/start -Djava.io.tmpdir = <my tmp folder path>启动app来解决问题 (3认同)