file-uploadspring-3

File upload using Spring 3


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 a beginner with 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

It's a bean page with commonsmultiparfile as class member

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();
    }

Solution

  • Change your controller class method to this and give it a try

    public String fileupload(HttpSession session,@ModelAttribute UploadFile uploadFile,BindingResult result){
    }
    

    I removed the HttpRequest and Response variables as they are not being used and moved the BindingResult object to be next to the ModelAttribute.

    Also try to include null checks in the code before accessing the elements.