玩! 2.1 Java完整的基本文件上传解决方案?
我的控制器中具有以下基本操作(出于说明目的),该基本操作已传递给MultipartFormData对象并检索图像和文件名.如果不为null,则将文件名打印到sbt控制台(确实如此),然后根据播放 http://www.playframework.com/documentation/2.1.0/JavaFileUpload 文档有望将其保存到file.renameTo中指定的目录中(女巫不是).
I have the following basic Action (for illustration purposes) in my controller that is passed a MultipartFormData object and retrieves the image and file name. If not null then prints the file name to the sbt console (witch it does) and then according to play http://www.playframework.com/documentation/2.1.0/JavaFileUpload documentation hopefully save it to the directory specified in file.renameTo (witch is does not).
public static Result uploadProductImage(
Http.MultipartFormData body
) {
Http.MultipartFormData.FilePart image = body.getFile("image");
if (image != null) {
String fileName = image.getFilename();
System.out.println(fileName);
File file = image.getFile();
file.renameTo(new File("/public/images/products", fileName));
}
return products();
}
我找不到任何文档的部分是您实际上如何将此文件移动到play项目的公用文件夹中.我注意到scala家伙具有功能
The part that I can't find any documentation on is how you actually move this file in to the public folder of the play project. I noticed the scala guys have the function
ref.moveTo()
我知道至少还有2到3个其他类似的问题,但是它们没有描述如何将文件移动到项目目录.我在这里根本上错过了一些非常简单的东西,但是我找不到它记录在网上的任何地方.
I know that there are at least 2 to 3 other questions that are similar but they do not describe how to move the file to a project directory. I'm missing something fundamentally very simple here but I cannot find it documented anywhere on the net.
经进一步检查,结果发现播放首先将上载的图像保存到系统特定的临时目录中,在我的情况下为/tmp/
.
On further inspection it turns out that play first saves the uploaded image to the system specific temporary directory that in my case was /tmp/
.
我的linux系统设置了三个分区:-
My linux system is set up with with three partitions:-
/boot
/
/home
play安装位于/home
分区上,从逻辑上与tmp
所在的/
分开.在这种情况下,分区被视为单独的文件系统.
The play installation is located on the /home
partition, logically separated from /
where tmp
resides. In this case the partitions are treated as separate file systems.
javadoc状态:-
javadoc states:-
重命名操作可能无法将文件从一个文件系统移动到另一个文件系统
这意味着File.renameTo()
可能无法在两个不同的分区,磁盘或文件系统上运行,这取决于系统.这是.renameTo()
失败的原因.
Meaning that depending on the system there's a chance that File.renameTo()
may not operate across two different partitions, disks or filesystems. This is the reason that .renameTo()
was failing.
解决方案:-
使用apache commons io FileUtils.moveFile()
Use apache commons io FileUtils.moveFile()
在Build.scala
中添加"org.apache.directory.studio" % "org.apache.commons.io" % "2.4"
val appDependencies = Seq(
javaCore, jdbc, javaJdbc, javaEbean,
"org.apache.directory.studio" % "org.apache.commons.io" % "2.4"
)
在播放控制台中,使用play clean compile
.
In the play console use play clean compile
.
如果使用IDEA play idea
public static Result uploadProductImage(
Http.MultipartFormData body
) {
Http.MultipartFormData.FilePart image = body.getFile("image");
if (image != null) {
String fileName = image.getFilename();
File file = image.getFile();
try {
FileUtils.moveFile(file, new File("public/images/products", fileName));
} catch (IOException ioe) {
System.out.println("Problem operating on filesystem");
}
}
return products();
}