播放框架 - 在mySql中上传文件 [英] Play framework - uploading file in mySql

查看:116
本文介绍了播放框架 - 在mySql中上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

解决方案

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


我会去保存数据库中的引用,并将图片上传到Web服务器的某处。或者,如果您坚持将图像保存在数据库中,请将其保存为拇指,这样可以保持数据库大小的可维护性,并且您的数据库大小可以接受。数据库是在我看来的数据,而不是资产,如图像。

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



我是如何做到的:

查看

确定你有正确的 enctype (这个是基于twitter引导的)

pre $


文件输入:

$ $ p $ $ code $ @ inputFile(taskForm(file1),_display - >Attachment,_label - > Messages(file))

在您的控制器中

  //首先,我想将文件附加到
MultipartFormData body =的任务。请求()主体()asMultipartFormData();
列表< FilePart> resourceFiles = body.getFiles();

然后通过附件迭代并上传到上传文件夹中:

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

文件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());

尝试{
FileUtils.copyFile(file,destinationFile);

TaskDocument taskDocument = new TaskDocument(newTask.description,/ assets / uploads /
+ newTask.getCode()+/+ i +_+ fileName,loggedInUsr,新任务);
taskDocument.save();
} catch(IOException e){
// TODO自动生成的catch块
e.printStackTrace();




结果



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

文件夹:T000345


  • 0_orange.png

  • 1_apple.png
  • 2_pear.png


编辑如果您收到有关commons软件包的错误,则必须在文件 Build中包含这个错误。 scala

  val appDependencies = Seq(
//在这里添加项目依赖关系,
mysql%mysql-connector-java%5.1.18,
org.specs2%%specs2%1.9%test,
commons-io%commons-io%2.2)//至少这个必须存在!


what is the simplest way to upload a file in mySql db in play 2.0 ?

解决方案

Uploading files in the database or in a upload folder and then save a link in the database?

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.

Uploading files is documented: http://www.playframework.org/documentation/2.0/JavaFileUpload

How I did it:

View

In the view, make sure you have the correct enctype (this one is based on the twitter bootstrap)

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

The file input:

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

In your controller

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

Then iterate trough the attachments and upload them to the upload folder:

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

Result

The code above result in creating a folder and placing the files in that folder. Example:

Folder: T000345

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

EDIT : 2012-06-23

If you receive the error about the commons package you have to include this in the file 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中上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆