数据库
首页 > 数据库> > 播放框架 – 在mySql中上传文件

播放框架 – 在mySql中上传文件

作者:互联网

在play 2.0中上传mySql数据库中文件的最简单方法是什么?

解决方法:

上传数据库或上传文件夹中的文件,然后在数据库中保存链接?

I would go for saving the reference in the database and uploading the image somewhere on your webserver. Or, if you persist on saving the image in the DB, save it as a thumb, this wil keep you database size maintainable and your db size acceptable. DBs are in my opinion for data and not assets like images.

上传文件记录在案:http://www.playframework.org/documentation/2.0/JavaFileUpload

我是怎么做到的

视图

在视图中,确保您具有正确的enctype(此基于twitter引导程序)

@helper.form(controllers.orders.routes.Task.save, 'class -> "form-horizontal", 'enctype -> "multipart/form-data")

文件输入:

@inputFile(taskForm("file1"), '_display -> "Attachment", '_label -> Messages("file"))

在你的控制器中

// first i get the id of the task where I want to attach my files to
MultipartFormData body = request().body().asMultipartFormData();
List<FilePart> resourceFiles = body.getFiles();

然后通过附件迭代并将它们上传到upload文件夹:

for (int i = 0; i < resourceFiles.size(); i++) {
    FilePart picture = body.getFile(resourceFiles.get(i).getKey());
    String fileName = picture.getFilename();
    File file = picture.getFile();

    File destinationFile = new File(play.Play.application().path().toString() + "//public//uploads//"
        + newTask.getCode() + "//" + i + "_" + fileName);
    System.out.println(play.Play.application().path());
    System.out.println(file.getAbsolutePath());

    try {
    FileUtils.copyFile(file, destinationFile);

    TaskDocument taskDocument = new TaskDocument(newTask.description, "/assets/uploads/"
        + newTask.getCode() + "/" + i + "_" + fileName, loggedInUsr, newTask);
    taskDocument.save();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
}

结果

上面的代码导致创建一个文件夹并将文件放在该文件夹中.例:

文件夹:T000345

> 0_orange.png
> 1_apple.png
> 2_pear.png

编辑:2012-06-23

如果您收到有关commons包的错误,则必须将其包含在Build.scala文件中:

val appDependencies = Seq(
// Add your project dependencies here,
"mysql" % "mysql-connector-java" % "5.1.18",
"org.specs2" %% "specs2" % "1.9" % "test",
"commons-io" % "commons-io" % "2.2") // at least this one must be present!

标签:mysql,file-upload,playframework,playframework-2-0
来源: https://codeday.me/bug/20190902/1788993.html