使用Java Play Framework网络上传jQuery文件错误:500

使用Java Play Framework网络上传jQuery文件错误:500,java,playframework-2.0,jquery-file-upload,Java,Playframework 2.0,Jquery File Upload,im使用Java Play框架和此文件上载插件:。阅读文档后,我在Java Play控制器中使用了此特定代码 public static Result upload() { File file = request().body().asRaw().asFile(); return ok("File uploaded"); } 我还将此路线添加到我的项目中: POST /upload controllers.Image.upload() 我

im使用Java Play框架和此文件上载插件:。阅读文档后,我在Java Play控制器中使用了此特定代码

public static Result upload() {
  File file = request().body().asRaw().asFile();
  return ok("File uploaded");
}
我还将此路线添加到我的项目中:

POST    /upload                     controllers.Image.upload()
我的视图模板:

@(scripts: Html)

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>jQuery File Upload Example</title>
</head>
<body>
<input id="fileupload" type="file" name="files[]" data-url="/upload">
@scripts
<script>
$(function () {
    $('#fileupload').fileupload({
        dataType: 'json',
        add: function (e, data) {
            data.context = $('<p/>').text('Uploading...').appendTo(document.body);
            data.submit();
        },
        done: function (e, data) {
            data.context.text('Upload finished.');
        }
    });
});
</script>
</body> 
错误是由控制器上载操作中的这一行引起的:

  File file = request().body().asRaw().asFile();

有人知道解决办法吗?谢谢您的帮助。

我想您可以以不同的方式访问上载文件。您可以使用以下内容:

    Http.MultipartFormData body = request().body().asMultipartFormData();

    for(Http.MultipartFormData.FilePart part : body.getFiles()){
        Logger.debug(part.getFilename());
        Logger.debug(part.getKey());
        Logger.debug(part.getContentType());
        Logger.debug(part.getFile().getName());
        Logger.debug(part.getFile().getAbsolutePath());
        Logger.debug(String.valueOf(part.getFile().getTotalSpace()));
    }

如果你有java.io.File实例,你可以做任何你想做的事情

实际上这是我的控制器部分,通过它我可以将文件从我的临时文件夹移动到应用程序公用文件夹,以便我可以进一步使用它。json将作为多部分表单数据返回。所以我们必须这样使用。希望这能解决你的问题

MultipartFormData body = request().body().asMultipartFormData();
        FilePart picture = body.getFile("file");
            if (picture != null) {
                File tempimg = picture.getFile();
                Path temp = tempimg.toPath();
                Path newFile = new File(Play.application().path().getAbsolutePath()+"/public/uploaded",picture.getFilename()).toPath();
                Files.move(temp, newFile);
                return ok("File uploaded");
            } else {
                flash("error", "Missing file");
                return badRequest("File Missing");    
            }

我也面临同样的问题。请求正文的类型为multipart,但没有指向它的文件。有什么解决办法吗?
MultipartFormData body = request().body().asMultipartFormData();
        FilePart picture = body.getFile("file");
            if (picture != null) {
                File tempimg = picture.getFile();
                Path temp = tempimg.toPath();
                Path newFile = new File(Play.application().path().getAbsolutePath()+"/public/uploaded",picture.getFilename()).toPath();
                Files.move(temp, newFile);
                return ok("File uploaded");
            } else {
                flash("error", "Missing file");
                return badRequest("File Missing");    
            }