【发布时间】:2019-11-25 14:52:57
【问题描述】:
我创建了一个简单的表单,以便将单个 PDF 文件上传到我的数据库中的 blob。我正在使用 thymeleaf + spring boot 和 dropzone.js。
我收到 405 错误 - 上传文件时方法不允许。
我已经尝试在我的控制器方法中添加一个@ResponseBody,但错误仍然存在。
这是我的 pdf-upload.html(百里香模板):
<html>
<head>
...
<script type="text/javascript">
Dropzone.options.myAwesomeDropzone = {
// url: "[[@{/vouchers/uploadPdfFile}]]",
paramName : "file", // The name that will be used to transfer the file
maxFilesize : 10, // MB
addRemoveLinks : true,
acceptedFiles : ".pdf",
maxFiles : 1,
// headers: {
// 'X-CSRF-TOKEN': $('meta[name="token"]').attr('content')
// }
};
</script>
....
</head>
....
<form th:action="@{/vouchers/uploadPdfFile}"
enctype="multipart/form-data"
method="post" class="dropzone needsclick dz-clickable"
id="my-awesome-dropzone">
<div class="col-12" id="dropzone">
<div class="dz-message needsclick">
Drop the PDF that will be attached to the Voucher...<br>
</div>
</div>
<input type="hidden" id="hd_voucher_id" th:value="${voucher.id}">
</form>
....
</html>
我的控制器如下所示:
@RequestMapping(value = "/uploadPdfFile", method = RequestMethod.POST)
public String uploadPdfPost(MultipartHttpServletRequest request) {
try {
Iterator<String> itr = request.getFileNames();
while (itr.hasNext()) {
String uploadedFile = itr.next();
MultipartFile file = request.getFile(uploadedFile);
String mimeType = file.getContentType();
String filename = file.getOriginalFilename();
byte[] bytes = file.getBytes();
FileUpload newFile = new FileUpload(filename, bytes, mimeType);
//fileUploadService.uploadFile(newFile);
}
} catch (Exception e) {
return "vouchers/pdf-upload";
}
return "vouchers/pdf-upload";
}
当提交表单时,我收到了我之前提到的 405 错误。
在日志控制台中,我收到以下错误:
2019-07-16 15:45:27 WARN o.s.w.s.m.s.DefaultHandlerExceptionResolver - Resolved [org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'POST' not supported]
2019-07-16 15:45:27 DEBUG o.s.web.servlet.DispatcherServlet - Exiting from "FORWARD" dispatch, status 405
2019-07-16 15:45:27 DEBUG o.s.s.w.c.SecurityContextPersistenceFilter - SecurityContextHolder now cleared, as request processing completed
2019-07-16 15:45:27 ERROR o.s.b.w.s.support.ErrorPageFilter - Cannot forward to error page for request [/vouchers/uploadPdfFile] as the response has already been committed. As a result, the response may have the wrong status code. If your application is running on WebSphere Application Server you may be able to resolve this problem by setting com.ibm.ws.webcontainer.invokeFlushAfterService to false
关于这里发生了什么的任何提示?
提前致谢 娟
【问题讨论】:
-
看起来 POST 方法没有注册。您能否检查同一控制器中的其他方法是否有效?如果不是,则控制器本身可能存在问题。如果其他人确实工作正常,您可以使用 Postman 独立测试该方法。
-
嗨,Nikhil,是的。还有一些其他 @RequestMappings 在同一个控制器中工作(包括 Get 和 Post )。实际上,如果我调试我的 uploadPdfPost() 方法,我将跳过方法行。
-
您不应该将 CSRF 令牌作为隐藏字段提交到表单中吗?
-
嗨 Catchwa,我有两个关于 CSRF 令牌的问题:1)Thymeleaf 自己在表单中添加了一个 csrf 令牌,2)如果您看到我的代码,则在初始化时尝试设置令牌Dropzone js 对象,但我无法解决问题。
-
Catchwa,这是生成的 HTML,您可以在其中看到 csrf 令牌:
标签: spring-boot thymeleaf dropzone.js http-status-code-405