我正在尝试将文件从客户端机器上传到服务器目录。我使用了以下代码:
文件上传.jsp
<form:form commandName="fileUpload" action="upload.action" method="post" enctype="multipart/form-data">
<form:label path="fileData">Upload a File</form:label> <br />
<form:input type="file" path="fileData" />
<input type="submit" value="upload" >
</form:form>
在我的控制器中:
@RequestMapping("/upload.action")
public String upload(@ModelAttribute("fileUpload") FileUpload fileUpload,HttpServletResponse response,Model model)
{
CommonsMultipartFile multipartFile = fileUpload.getFileData();
String orginalName = multipartFile.getOriginalFilename();
String filePath = "/my_uploads/"+orginalName;
File destination = new File(filePath);
String status ="success";
try {
multipartFile.transferTo(destination);
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
status="failure";
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
status="iofailure";
}
model.addAttribute("status", status);
return "home";
}
文件上传.java:
{
private CommonsMultipartFile fileData;
....
}
NullPointerException
被扔在线上String orginalName = multipartFile.getOriginalFilename();
..我做错了什么?