播放2.0.4文件上传。 NullPointerException:null [英] Play 2.0.4 file upload. NullPointerException: null

查看:181
本文介绍了播放2.0.4文件上传。 NullPointerException:null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Java中使用Play,并且处理文件上传时遇到问题。



我做了一切像这个页面,但当我提交表单时,我得到 NullPointerException



这是我的表单: >

  @form(action = routes.Application.upload,'enctype  - >multipart / form-data){
< input type =filename =picture>
< p>
< input type =submit>
< / p>

$ / code>

路线:

 POST / upload controllers.Application.upload()

是我的控制器:

  import play.mvc.Http.MultipartFormData; 
import play.mvc.Http.MultipartFormData.FilePart;
$ b public static Result upload(){
MultipartFormData body = request()。body()。asMultipartFormData();
FilePart picture = body.getFile(picture); //这里我得到了NullPointerException
if(picture!= null){
String fileName = picture.getFilename();
String contentType = picture.getContentType();
档案file = picture.getFile();
return ok(File uploaded);
} else {
flash(error,Missing file);
返回重定向(routes.Application.index());


$ / code $ / pre

我已经尝试过两种解决方案但没有人帮助我。



我该如何解决这个问题? 请记住, NullExceptions 在这个地方是 body 不是图片



唯一可能的原因是您使用的HTML表单没有发送 enctype =multipart / form-data(也许你没有在浏览器中刷新表单,仍然试图发送一个正常的表单?)

请确保(在您的浏览器中),该页面

 < form action =/ uploadmethod = POSTenctype =multipart / form-data> 

< input type =filename =picture>

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

< / form>


I'm using Play in Java and I have problem with Handling file upload.

I made everything like on this page but I get NullPointerException when I submit the form.

Here is my form:

@form(action = routes.Application.upload, 'enctype -> "multipart/form-data") {
<input type="file" name="picture">
    <p>
       <input type="submit">
   </p>
}

Route:

POST    /upload         controllers.Application.upload()

Here is my controller:

import play.mvc.Http.MultipartFormData;
import play.mvc.Http.MultipartFormData.FilePart;

public static Result upload() {
    MultipartFormData body = request().body().asMultipartFormData();
    FilePart picture = body.getFile("picture");  //here i got NullPointerException
    if (picture != null) {
        String fileName = picture.getFilename();
        String contentType = picture.getContentType(); 
        File file = picture.getFile();
        return ok("File uploaded");
    } else {
        flash("error", "Missing file");
        return redirect(routes.Application.index());    
    }
}

I've tried couple of solutions found on the Internet but none of them helped me.

How can I fix it?

解决方案

Keep in mind that NullExceptions in this place is for body not for picture

The only possible reason is that you are sending it with HTML form which hasn't enctype="multipart/form-data" (maybe you didn't refresh the form in the browser and still trying to send a normal form?)

make sure (in your browser), that page where you're filling the form has this form declaration and try again.

<form action="/upload" method="POST" enctype="multipart/form-data">

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

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

</form>

这篇关于播放2.0.4文件上传。 NullPointerException:null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆