我尝试在application.conf文件中指定attachments.path属性,但这没有任何效果。
在play 2.0.1的文档中,我没有找到任何解释如何更改上传文件目录的内容。
我错过了什么吗?
答案 0 :(得分:10)
虽然application.conf
中没有此类变量,但您可以轻松添加并在您的方法中使用。按照您的意愿调用,即:
application.conf
中的新行:
myUploadPath = "/home/your-account/some/custom/upload/folder/"
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
检查,以防止随机覆盖。