【发布时间】:2016-11-27 16:31:03
【问题描述】:
我想在我的网站上上传文件/图像。 实际上我可以上传我的文件,但有时我的页面上有错误,我认为这不是这样做的好方法。
我的代码:
private String uploadImage()
{
String result = f.validate("avatar");
if(result.isEmpty())
return "none";
Http.MultipartFormData.FilePart<File> file = body.getFile("avatar");
if(file == null)
return "File is empty or not exist.";
String filename = file.getFilename();
/* Debug file file*/
Logger.debug("nameFile " + file.getFilename());
Logger.debug("contentFile " + file.getContentType());
/* End debug*/
File f = file.getFile();
/* Debug file f*/
String name = f.getName();
long totalSpace = f.getTotalSpace();
Logger.debug("nameF " + name);
Logger.debug("sizeF " + totalSpace);
/* End debug*/
try {
/* File user is not f file variable*/
File fileUser = getFileUser(filename);
Try.of(fileUser::delete);
/* Use of org.apache.commons.io*/
FileUtils.moveFile(f, getFileUser(filename));
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
private File getFileUser(String fileName)
{
return new File("public/upload/avatars",
user.getId().toString() + "." + FilenameUtils.getExtension(fileName));
}
private boolean fileIsPicture(File f)
{
String mimetype = new MimetypesFileTypeMap().getContentType(f);
String type = mimetype.split("/")[0];
return (type.equals("image"));
}
错误:
java.io.FileNotFoundException: Source 'C:\Users\Alexis\AppData\Local\Temp\playtemp1188878075698565675\multipartBody8627595002998720963asTemporaryFile' does not exist
但我已经调试了一些信息(为什么要调试 2 次?):
[info] play.api.Play - Application started (Dev)
[info] application - avatar null
[debug] application - nameFile KwizzyPicture.jpg
[debug] application - contentFile image/jpeg
[debug] application - nameF multipartBody1959853090277810547asTemporaryFile
[debug] application - sizeF 763679993856
[info] application - avatar null
[debug] application - nameFile KwizzyPicture.jpg
[debug] application - contentFile image/jpeg
[debug] application - nameF multipartBody1959853090277810547asTemporaryFile
[debug] application - sizeF 0
感谢您的帮助! 我是法国人,很抱歉犯了错误。
【问题讨论】:
-
粘贴引发 FileNoutFoundException 的完整堆栈跟踪。
-
好的,等几分钟我回来
-
没人能帮我吗?
标签: java image playframework image-uploading