我只是游戏框架中的新手,我正在尝试使用 playframework 2.1.1 对上传文件进行编码,但我收到了这个异常 [NullPointerException]: null
我的控制器中有以下代码并使用此链接
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");
}
}
并在 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>
}
请提出一些建议,我错在哪里。
提前致谢