简体   繁体   English

使用Java Play Framework进行jQuery-File-Upload NetworkError:500

[英]jQuery-File-Upload with Java Play Framework NetworkError: 500

im using Java Play Framework and this File-Upload-Plugin: http://blueimp.github.io/jQuery-File-Upload/ . 我正在使用Java Play Framework和这个File-Upload-Plugin: http//blueimp.github.io/jQuery-File-Upload/ After read the documentation http://www.playframework.com/documentation/2.0/JavaFileUpload i used this specific code in my Java Play Controller. 阅读文档http://www.playframework.com/documentation/2.0/JavaFileUpload后,我在Java Play Controller中使用了这个特定的代码。

public static Result upload() {
  File file = request().body().asRaw().asFile();
  return ok("File uploaded");
}

I've also added this route to my project: 我还将这条路线添加到我的项目中:

POST    /upload                     controllers.Image.upload()

My View-Template: 我的视图 - 模板:

@(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> 

Now if i upload a image firebug shows me the following error: 现在,如果我上传图像,firebug会显示以下错误:

"NetworkError: 500 Internal Server Error - http://localhost:9000/upload"

The error was caused by this one line in the Controller Upload-action: 该错误是由Controller Upload-action中的这一行引起的:

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

Anyone know a solution? 有人知道解决方案吗? Thank you for your help. 谢谢您的帮助。

I guess you can access your upload files differently. 我想你可以用不同的方式访问你的上传文件。 You can use something like: 您可以使用以下内容:

    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()));
    }

One you have your java.io.File instance you can do whatever you want 你有java.io.File实例,你可以做任何你想做的事

Actually this is my controller part by which i can move the file from my temp folder to the application public folder so that i can use it further. 实际上这是我的控制器部分,通过它我可以将文件从我的临时文件夹移动到应用程序公用文件夹,以便我可以进一步使用它。 the json will be returned as a multipart form data. json将作为多部分表单数据返回。 so we have to use like this. 所以我们必须这样使用。 Hope this solves your problem. 希望这能解决你的问题。

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");    
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM