java - Spring 文件上传 etat HTTP 405 - 不支持请求方法 'POST'

标签 java spring spring-mvc file-upload

我正在尝试制作一个上传文件表单,将文件上传到数据库并将文件复制到指定位置,但当我上传文件时,我总是收到这个恼人的错误Etat HTTP 405 - 请求方法“POST”不支持

这是我在 Controller 中的方法

 @RequestMapping(value = "/singleUploadimage", method = RequestMethod.GET)
    public String getSingleUploadPage(ModelMap model) {
        FileBucket fileModel = new FileBucket();
        Evennement e= new Evennement();
        model.addAttribute("fileBucket", fileModel);
        model.addAttribute("evennement", e);
        model.addAttribute("listequipe", this.equipeervice.findAll());
        return "testupload";    }

    @RequestMapping(value = "/singleUploadimage", method = RequestMethod.POST)
    public String singleFileUpload(@Valid FileBucket fileBucket,
            BindingResult result, ModelMap model,@ModelAttribute("evennement") Evennement p) throws IOException {


            System.out.println("Fetching file");
            MultipartFile multipartFile = fileBucket.getFile();

            // Now do something with file...
            FileCopyUtils.copy(fileBucket.getFile().getBytes(), new File( UPLOAD_LOCATION + fileBucket.getFile().getOriginalFilename()));
            String fileName = multipartFile.getOriginalFilename();
            p.setImage(fileName);
            this.Evennementervice.addEvennement(p);

            model.addAttribute("fileName", fileName);
            return "testupload";    
    }

我的表格

    <form:form method="POST" modelAttribute="evennement"
                enctype="multipart/form-data">
                <div class="row">
                    <div>
                        <label for="file">Upload a file</label>
                        <div class="col-md-7">
                            <form:input type="file" path="file" id="file" />
                            <div class="has-error">
                                <form:errors path="file" class="help-inline" />
                            </div>
                        </div>
                    </div>
                </div>
...
    <div class="row">
                <div class="form-actions floatRight">
                    <input type="submit" value="Upload" class="btn btn-primary btn-sm">
                </div>
            </div>

这是tomcat上的日志错误

44846 [http-bio-8080-exec-520] WARN org.springframework.web.servlet.PageNotFound
 - Request method 'POST' not supported
44846 [http-bio-8080-exec-520] WARN org.springframework.web.servlet.mvc.support.
DefaultHandlerExceptionResolver - Handler execution resulted in exception: Reque
st method 'POST' not supported

我还将多部分解析器添加到 web.xml

我不明白我做错了什么,非常感谢任何帮助。

最佳答案

实际上,我通过在表单中​​添加 action="/singleUploadimage?${_csrf.parameterName}=${_csrf.token}"

解决了这个问题

关于java - Spring 文件上传 etat HTTP 405 - 不支持请求方法 'POST',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37864839/

相关文章:

model-view-controller - Autowiring 的 bean 在 MVC Controller 中为空

java - Spring MVC 不使用 angularjs 指令渲染 html

jquery - 你如何访问 jquery 中的模型属性?

java - 使用逗号分隔值但保留括号内使用的逗号的正则表达式模式

java - 如何消费Spring Cloud Stream中默认死信队列的消息?

java - Scala 和 Java 中的 RoundingMode.HALF_UP 区别

java - 如何测试 @Transactional 方法,其中两个表正在更新

java - spring boot 返回字符串而不是 .html 文件

java - SimpleJdbcCall异常: Required input parameter is missing

Java秒表类