PlayFramework - 如何上传文件/图像

时间:2016-11-27 16:31:03

标签: java image playframework image-uploading

我想在我的网站上传文件/图片。 实际上我可以上传我的文件,但有时我的页面上有错误,我认为这不是这样做的好方法。

我的代码:

java.io.FileNotFoundException: Source 'C:\Users\Alexis\AppData\Local\Temp\playtemp1188878075698565675\multipartBody8627595002998720963asTemporaryFile' does not exist

错误:

[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

但是我调试了一些信息(为什么要调试2次?):

<footer>
<a  id="footer_" href="#" onclick="openNav()">&#9776; Over ons </a>
</footer>

Full code here

感谢您的帮助! 我是法国人,对于错误感到抱歉。

1 个答案:

答案 0 :(得分:0)

您必须使用FileInputStream和FileOutptStream类

  Http.MultipartFormData.FilePart<File> file = body.getFile("avatar");

 FileInputStream in = null;
  FileOutputStream out = null;
  try {
     in = new FileInputStream(file);
     //enter the file location in server
     out = new FileOutputStream("output.jpeg");

     int c;
     while ((c = in.read()) != -1) {
        out.write(c);
     }
  }finally {
     if (in != null) {
        in.close();
     }
     if (out != null) {
        out.close();
     }
  }