播放框架 – 在mySql中上传文件
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了播放框架 – 在mySql中上传文件,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2178字,纯文字阅读大概需要4分钟。
内容图文
![播放框架 – 在mySql中上传文件](/upload/InfoBanner/zyjiaocheng/892/88bf64ab7723426495da9e8a174a3cd8.jpg)
在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中上传文件全部内容,希望文章能够帮你解决播放框架 – 在mySql中上传文件所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。