0

I want to upload an image file to one location in my computer with Spring MVC. But when I chose the file I am getting NullPointerException error. Here is my codes;

@RequestMapping("/save-product")
public void saveFile(HttpServletRequest servletRequest,
        @ModelAttribute("uploadedFile") UploadedFile uploadedFile,
        BindingResult bindingResult, Model model) {

    MultipartFile multipartFile = uploadedFile.getMultipartFile();
    String fileName = multipartFile.getOriginalFilename();
    try {
        File file = new File(servletRequest.getServletContext().getRealPath("/home/mesud/springupload/"), fileName);
        multipartFile.transferTo(file);
    } catch (IOException e) {
        e.printStackTrace();
    }
}


<sf:form enctype="multipart/form-data" modelAttribute="uploadedFile">
                <fieldset >
                 <legend>Resim Yükle</legend>
                 <div id='progressBar'
                    style='height: 20px; border: 2px solid green; margin-bottom: 20px'>
                    <div id='bar' style='height: 100%; background: #33dd33; width: 0%'>
                    </div>
                </div>
                <input type="file" id=profilResim value="Profil Resmi" style="margin-bottom: 20px"/><br/>
              </fieldset>
</sf:form>

I am uploading file in onSelect function in Javascript. Here is my javascript method;

function uploadNext() {
        var xhr = new XMLHttpRequest();
        var fd = new FormData();
        var file = document.getElementById('profilResim').files[filesUploaded];
        fd.append("multipartFile", file);
        xhr.upload.addEventListener("progress", onUploadProgress, false);
        xhr.addEventListener("load", onUploadComplete, false);
        xhr.addEventListener("error", onUploadFailed, false);
        xhr.open("POST", "save-product");
        debug('uploading ' + file.name);
        xhr.send(fd);
    }

In my Applicaiton Initializer class I use this method for multipart requests;

@Override
protected void customizeRegistration(Dynamic registration) {
    registration.addMapping("/");
    registration.setMultipartConfig(new   MultipartConfigElement("/home/mesud/springtemp",2097152, 4194304, 0));
}

Why my UploadedFile object (this object is includes a MultipartFile object) is posted null?

1 Answer 1

1

There could be several reasons . Do some checks like have you defined CommonsMultiPartResolver bean in your application-context.xml file . If not define it . Also check your jars . Ensure you have commons-fileupload jar and commons-io jar . Let me know if that helped you.

Sign up to request clarification or add additional context in comments.

7 Comments

I have controlled these. I have defined CommonsMultipartResolver in my xml and I have commons-fileupload and commons-io.But again the same error :-(
Can you edit your question and paste your XML file contents ?
I use annotation based settings. So I updated the question
I think your multipartconfig is messed it should not necessarily be defined in initializer file and it should be bean annotated , try something like this. @Bean(name = "multipartResolver") public CommonsMultipartResolver createMultipartResolver() { CommonsMultipartResolver resolver=new CommonsMultipartResolver(); resolver.setDefaultEncoding("utf-8"); return resolver; }
It's better you use XML based settings first , and when you understand them, implement non-xml settings.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.