我正在尝试制作一个上传文件表单,将文件上传到数据库并将文件复制到指定位置,但当我上传文件时,我总是收到这个恼人的错误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}"
解决了这个问题
我是一名优秀的程序员,十分优秀!