gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 12:30:45 24 4
gpt4 key购买 nike

我正在尝试制作一个上传文件表单,将文件上传到数据库并将文件复制到指定位置,但当我上传文件时,我总是收到这个恼人的错误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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com