[英]problem when upload file in play framework
I tried uploading a file, and placed the file in the "public / images" directory and it worked, but the file I uploaded was zero in size and certainly couldn't be opened我尝试上传文件,并将文件放在“公共/图像”目录中,它可以工作,但我上传的文件大小为零,当然无法打开
public Result upload() throws IOException {
Http.MultipartFormData<File> requestBody = request().body().asMultipartFormData();
Http.MultipartFormData.FilePart<File> profile_pic = requestBody.getFile("profile_pic");
String dirPath = "public/images/";
if(profile_pic != null) {
String name = profile_pic.getFilename();
File file = profile_pic.getFile();
System.out.println(file);
File theDir = new File(dirPath);
if (!theDir.exists()) {
boolean result = false;
try {
theDir.mkdirs();
result = true;
} catch (SecurityException se) {
// handle it
}
if (result) {
System.out.println("DIR created");
}
}
try {
File filex = new File(dirPath + name.toLowerCase());
filex.createNewFile();
file.renameTo(filex);
}catch (Exception e) {
// TODO: handle exception
}
return ok("File uploaded ");
}else {
return badRequest("Eroor");
}
}
I think creating a new file and renaming your old file to that name may cause trouble.我认为创建一个新文件并将旧文件重命名为该名称可能会造成麻烦。 I would recommend using the move
method, see docs here .我建议使用move
方法,请参阅此处的文档。
Does your System.out.println(file);
你的System.out.println(file);
print what looks like a normal png file?打印看起来像普通 png 文件的东西?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.