简体   繁体   English

NullPointerException:null playframework 2.1.1文件上传

[英]NullPointerException: null playframework 2.1.1 file upload

i am just new-ish in play framework, I was Trying to code upload file with playframework 2.1.1, but I am getting this exception [NullPointerException]: null 我只是玩游戏框架中的新手,我正尝试使用playframework 2.1.1编写上传文件的代码,但出现此异常[NullPointerException]:null
I have following code in My Controller and using this link 我的控制器中有以下代码,并使用此链接

package controllers;

//import com.ning.http.client.FilePart;
import play.mvc.Http.MultipartFormData;
import play.mvc.Http.MultipartFormData.FilePart;
import play.*;
import play.mvc.*;
import play.data.*;
import static play.data.Form.*;
import java.io.File;
import models.*;
import views.html.*;

public class Application extends Controller {

    public static Result index() {
        return ok(index.render("My App"));
    }
public static Result upload(){
    MultipartFormData body=request().body().asMultipartFormData();
    MultipartFormData.FilePart picture=body.getFile("picture");//Error is here 

    if(picture!=null){
        //String fileName=picture.getFileName(); and if I uncomment this line it also show an error for 'value not find 'getFileName' ' is there any import is needed?
        String contentType=picture.getContentType();
        File file=picture.getFile();
        return ok("File Uploaded");
    }
    else 
    {
        flash("error", "Missing File");
        return redirect(routes.Application.index());
    }
//File file = request().body().asRaw().asFile();
//return ok("File uploaded");

}  

}

and following code in View that is app/views/upload.scala.html 并在View中的以下代码是app / views / upload.scala.html

@helper.form(action = routes.Application.upload, 'enctype -> "multipart/form-data") {

    <input type="file" name="picture">

    <p>
        <input type="submit">
    </p>

}

please give some suggestion that where i am wrong. 请给我一些建议,我错了。

Thanks in Advance 提前致谢

You didn't post the routes file, but by the error I guess that you are mapping the request to a GET instead of POST . 您没有发布routes文件,但是由于错误,我猜您正在将请求映射到GET而不是POST

The reasoning is that you get a NullPointerException when calling a method in body . 原因是在body调用方法时会收到NullPointerException body is initialized in the previous line from the request object, retrieving the contents as multipartFormData . body是从request对象的上一行初始化的,将内容检索为multipartFormData

Your form snippet properly declares the form as multipart/form-data and maps to the controller's method as expected. 您的表单片段正确地将表单声明为multipart/form-data ,并按预期映射到控制器的方法。 This means that the only reason why you shouldn't get the contents is that the request has an empty body, and in this scenario that would only be in a GET request. 这意味着您不应该获取内容的唯一原因是该请求的正文为空,在这种情况下,该请求只能在GET请求中。

Of course, there may be a more exotic reason, but I bet that's the one. 当然,可能还有一个更奇特的原因,但是我敢打赌这就是原因。

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

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