我在Java中使用Play,並且遇到了處理文件上傳的問題。播放2.0.4文件上傳。 NullPointerException:null
我做了一切就像this頁面,但我提交表單時得到NullPointerException
。
這裏是我的形式:
@form(action = routes.Application.upload, 'enctype -> "multipart/form-data") {
<input type="file" name="picture">
<p>
<input type="submit">
</p>
}
路線:
POST /upload controllers.Application.upload()
這裏是我的控制器:
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());
}
}
我試過幾個解決方案的互聯網,但沒有發現上他們幫助了我。
我該如何解決?
奇怪,代碼是正確的,你也有它的(我檢查行由行)沒有錯誤。你在開發模式下工作嗎? maby你在生產模式下開始應用程序,它不反映代碼的變化,嘗試用ctrl + c在控制檯中停止你的應用程序,並再次用'play run'運行它,然後再次檢查表單。向我們展示其他進口產品(也許您偶然使用了一些Scala版本......)如果可能的話,請將您的代碼在現狀中發佈,因爲它現在是 – biesior
其餘進口產品: 'import java.io.File;' '進口play.data *;'' 進口play.mvc *;' 關於代碼的其餘部分它基本上是從PlayFramework網頁教程與觀點無關的變化只是簡單的應用程序TodoList的。我需要在其他應用程序中上傳文件,但我只是在這個文件上進行測試。我使用了'body.asFormUrlEncoded()',並且我注意到'input type =「file」'中的值不在該映射中,而是來自'input type =「text」'的值是。看起來提交按鈕不會向前傳遞圖片。我正在使用Play 2.0.4。 – Gibson001
這也是正確的,文件(S)的'asMultipartFormData提供()',TBH,我不知道什麼是錯的,如果你不能發佈整個項目 - 嘗試創建新的應用程序,並把你上傳的代碼而已,它會可能運行沒有問題,那麼你就可以自己檢查,如果有電流和其他代碼之間存在一些差異。 – biesior