我尝试做Java 文件上传教程,但遇到了这个问题。
我的文件被上传到in 的tmp
文件夹中File System
,Linux
如下所示:
/tmp/multipartBody7066610990481359884asTemporaryFile
我没有理想的更改上传文件的路径,所以我将其复制/myproject/public/upload/
到Java code
. 之后我得到了这个错误:
[FileNotFoundException: /home/thangnk/demo/public/upload (Is a directory)]
我仔细检查了这个错误,我确定该upload
文件夹已经创建。所以任何人都可以告诉我如何解决它?
这是我的代码:
---应用程序.java
public class Application extends Controller {
public static Result index() {
return ok(index.render("Your new application is ready."));
}
public static Result upload() throws IOException {
MultipartFormData body = request().body().asMultipartFormData();
FilePart picture = body.getFile("picture");
if (picture != null) {
String fileName = picture.getFilename();
String contentType = picture.getContentType();
File file = picture.getFile();
String orig = file.getAbsolutePath();
String dest = "/home/thangnk/demo/public/upload/";
//Copy file
InputStream in = new FileInputStream(orig);
OutputStream out = new FileOutputStream(dest);
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
return ok("File uploaded");
} else {
flash("error", "Missing file");
return redirect(routes.Application.index());
}
}
}
还有一个问题。在 Play Framework 2.0 中上传图片最简单的方法是什么?
谢谢阅读!!!