Подтвердить что ты не робот

Обязательный параметр "файл" параметра MultipartFile отсутствует в spring mvc

Я пытаюсь добавить функцию загрузки изображения в мое приложение spring mvc.

часть jsp:

...
<form method="POST"  action="uploadImage" enctype="multipart/form-data">
                <div class="load-line">
                    <input type="file" class="file"/>
                    <input type="submit" value="Upload">
...

конфигурации:

... 
<bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />
...

контроллер:

 @RequestMapping(value="/member/createCompany/uploadImage", method=RequestMethod.POST)
    public @ResponseBody String handleFileUpload(
            @RequestParam("file") MultipartFile file){
        String name = "image_name";
        if (!file.isEmpty()) {
            try {
                byte[] bytes = file.getBytes();
                BufferedOutputStream stream =
                        new BufferedOutputStream(new FileOutputStream(new File(name + "-uploaded")));
                stream.write(bytes);
                stream.close();
                return "You successfully uploaded " + name + " into " + name + "-uploaded !";
            } catch (Exception e) {
                return "You failed to upload " + name + " => " + e.getMessage();
            }
        } else {
            return "You failed to upload " + name + " because the file was empty.";
        }
    }

После того, как я выбрал картинку, нажмите кнопку "Загрузить" и вы увидите сообщение об ошибке:

HTTP Status 400 - Required MultipartFile parameter 'file' is not present

Чем я ошибаюсь?

4b9b3361

Ответ 1

Вы не указали атрибут name, @RequestParam("textFile") требует имя,

 <input type="file" class="file" name="textFile"/>

Ответ 2

добавить атрибут имени в тег ввода "файл"

<input type="file" class="file" name="file"/>

Ответ 3

Для тех, кто не может найти подходящее решение, не забудьте добавить зависимость тимелеафа к вашему проекту

Например;

compile("org.springframework.boot:spring-boot-starter-thymeleaf")