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

时间:2012-06-08 20:05:58

标签: java playframework playframework-2.0

我尝试在application.conf文件中指定attachments.path属性,但这没有任何效果。

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

我错过了什么吗?

1 个答案:

答案 0 :(得分:10)

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

application.conf中的新行:

myUploadPath = "/home/your-account/some/custom/upload/folder/"

根据documentation sample

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

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