i'm uploading an image file from this page and i'm getting nullpointer exception for multipartfile creation , i'm unable to understand where i did mistake? and i'm newbie for java
fileupload.jsp
<form modelAttribute="uploadFile" name="frm" method="post"
enctype="multipart/form-data" onSubmit="return Validate();">
<form:label for="fileData" path="fileData">File</form:label>
<input path="fileData" id="image" type="file" />
<input type="submit" value="Upload" />
</form>
UploadFile.java
它是一个以 commonsmultiparfile 作为类成员的 bean 页面
public class UploadFile {
private String filename;
private CommonsMultipartFile fileData;
/**
* @return the filename
*/
public String getFilename() {
return filename;
}
/**
* @param filename the filename to set
*/
public void setFilename(String filename) {
this.filename = filename;
}
/**
* @return the fileData
*/
public CommonsMultipartFile getFileData() {
return fileData;
}
/**
* @param fileData the fileData to set
*/
public void setFileData(CommonsMultipartFile fileData) {
this.fileData = fileData;
}
}
FileUploadController
@RequestMapping(value = "/fileUpload", method = RequestMethod.POST)
public String fileupload(
serviceOrder,HttpSession session,
ModelMap model, HttpServletRequest request,UploadFile uploadFile,
HttpServletResponse response, Object command, BindingResult result) throws Exception {
if (result.hasErrors()) {
for (ObjectError error : result.getAllErrors()) {
logger.info("Error: " + error.getCode() + " - "
+ error.getDefaultMessage());
}
return "//fileUpload";
}
try{
MultipartFile multipartFile = uploadFile.getFileData();
InputStream inputStream = null;
OutputStream outputStream = null;
logger.info("---------------"+uploadFile);
logger.info("---------------------------"+multipartFile);
if (multipartFile.getSize() > 0) {
inputStream = multipartFile.getInputStream();
// File realUpload = new File("C:/");
outputStream = new FileOutputStream("D:\\Images\\"
+ multipartFile.getOriginalFilename());
logger.info("Original File Name"+multipartFile.getOriginalFilename());
int readBytes = 0;
byte[] buffer = new byte[8192];
while ((readBytes = inputStream.read(buffer, 0, 8192)) != -1) {
logger.info("writing data into file.....");
outputStream.write(buffer, 0, readBytes);
}
outputStream.close();
inputStream.close();
session.setAttribute("uploadFile", "D:\\Images\\"
+ multipartFile.getOriginalFilename());
}
} catch(Exception e) {
e.printStackTrace();
}
我正在从这个页面上传一个图像文件,我在创建多部分文件时遇到了空指针异常,我无法理解我在哪里做错了?我是java的新手