Spring File Upload Internal server error

2019-05-25 02:40发布

问题:

I'm trying to upload a file using spring mvc. My form:

    <form enctype="multipart/form-data">
       Name: <input type="text" name=name><br> 
       Email ID: <input type="email" name=emailID><br> 
       Attachment: <input type="file" name=file id="file"><br> 
       Submit?: <input type="submit" name=submit value="Done"><br>
    </form>

I get an internal server error.

The error is:

1)505 Error

2)No Stack Trace

The form resets itself rather than go to the next page which is a notice page.

回答1:

To be safe you should always use POST or PUT when doing uploads else you are going to lose characters along the way as only ASCII is supported then.

<form enctype="multipart/form-data" method="POST">

Make sure that you have Spring configured correctly for file uploads, meaning that you have setup a MultipartResolver, without it Spring is not going to support file uploads.

Your controller should use data-binding, instead of doing it yourself (use the framework).

@RequestMapping(value = "/add", params = "submit", method = RequestMethod.POST)
public ModelAndView addForm(@ModelAttribute PostModel newPost){
    System.out.println("Added " + newPost.getQuestion() + " successfully");
    System.out.println("Added the file " + file.getName() + "successfully");
    return addController.showNotice(newPost.getQuestion());
}