I am trying to upload a file to the server directory from client machine. I used the following codes :
FileUpload.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>
In my Controller:
@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";
}
FileUpload.java :
{
private CommonsMultipartFile fileData;
....
}
NullPointerException
is thrown at the line String orginalName = multipartFile.getOriginalFilename();
.. what wrong thing i have done??
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…