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();
}
このページから画像ファイルをアップロードしていますが、マルチパートファイルの作成で nullpointer 例外が発生しました。どこを間違えたのか理解できません。そして私はJavaの初心者です