Java - file upload problem

<>

This question already has an answer here:

Answers


You're not actually submitting the form. You're navigating to the page with a GET request.

Replace your "Submit" anchor with a submit button:

<button type="submit" class="jUiButton">Submit</button>

You could keep the <a> but then you would have to use JavaScript to submit the form manually.


You should not read the HttpServletRequest#getInputStream() beforehand. It can be read only once. The Commons FileUpload cannot read it anymore if you have read it yourself beforehand. Get rid of all those lines in your servlet until ServletFileUpload#isMultipartContent() line.


The guide you are following is out of date (2008). If this is a new project you might want to start with an annotation based approach. This guide might be better to follow (2010). A file upload controller would then look like:

@Controller
public class FileUploadContoller {   
    @RequestMapping(value = "/fileupload", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public String ingest(@RequestParam("file") final MultipartFile file) throws Exception {
        if (file.isEmpty()) {
            System.out.println("empty");
        } else {
            System.out.println("not empty");
        }

        // do something with file.getBytes()

        return("ok");
    }
}

This is only the controller and you will need to add the appropriate Spring configuration. I could help further if you want to go down this route.


Need Your Help

Building a generic XML parser in Python?

python xml parsing elementtree celementtree

I am a newbie and having 1 week experience writing python scripts.

Exception thrown while deleting entity inside of service: org.hibernate.LazyInitializationException: could not initialize proxy - no Session

java spring hibernate transactions

I am using HibernateTemplate in my spring application. when ever I try to call a operation like save, update or delete , then I get this as exception.