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

时间:2016-06-16 16:37:42

标签: java spring spring-mvc file-upload

我试图创建一个上传文件表单,将文件上传到数据库并将文件复制到指定位置,但是当我上传文件时,我总是遇到这个恼人的错误Etat HTTP 405 - Request method 'POST' not supported

以下是我在控制器中的方法

 @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

我不知道自己做错了什么,非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

实际上我通过添加action="/singleUploadimage?${_csrf.parameterName}=${_csrf.token}"

形式解决了这个问题