0
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的新手

4

2 回答 2

0

您是否定义了 multipartresolver bean

例如,您可以在 "<"bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" /">" 中声明 bean

于 2012-11-08T08:09:55.143 回答
0

将您的控制器类方法更改为此并尝试一下

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

}

我删除了 HttpRequest 和 Response 变量,因为它们没有被使用,并将 BindingResult 对象移动到 ModelAttribute 旁边。在访问元素之前,还要尝试在代码中包含空检查。

于 2012-11-08T17:35:21.077 回答