英文:
Spring not accepting list of multipart files: java.lang.NoSuchMethodException: org.springframework.web.multipart.MultipartFile
问题
我通过AJAX发送了这个:
var formData = new FormData();
var totalfiles = document.getElementById('files').files.length;
for (var index = 0; index < totalfiles; index++) {
formData.append("files", document.getElementById('files').files[index]);
}
而且在我的Spring 4应用程序中,它应该被这个方法接收:
@RequestMapping(value = "/mapUploads/submit", method = RequestMethod.POST)
protected void check(HttpServletRequest request, List<MultipartFile> files)
但由于某些原因,Spring 4告诉我无法实例化该bean:
java.lang.NoSuchMethodException: org.springframework.web.multipart.MultipartFile
在调试模式下,甚至没有进入方法参数。
英文:
I was sending this via AJAX:
var formData = new FormData();
var totalfiles = document.getElementById('files').files.length;
for (var index = 0; index < totalfiles; index++) {
formData.append("files", document.getElementById('files').files[index]);
}
And with my Spring 4 application, it should have been received by this method:
@RequestMapping(value = "/mapUploads/submit", method = RequestMethod.POST)
protected void check(HttpServletRequest request, List<MultipartFile> files)
But for some reason, Spring 4 was telling me that the bean could not be instantiated:
java.lang.NoSuchMethodException: org.springframework.web.multipart.MultipartFile
In debugging mode, it wasn't even entering the method parameter.
答案1
得分: 0
将方法签名更改为以下内容:
@RequestMapping(value = "/mapUploads/submit", method = RequestMethod.POST)
protected void check(HttpServletRequest request) {
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
List<MultipartFile> files = multipartRequest.getFiles("files");
}
这个问题在这里得到了解决。但在那个问题中,问题提出者只在参数中使用了一个文件 - 实际上可以通过在参数签名中添加MultipartFile
来解决:
@RequestMapping(value = "/mapUploads/submit", method = RequestMethod.POST)
protected void check(HttpServletRequest request, List<MultipartFile> files) {
这是我之前的情况,但是当我决定接受文件列表时,我遇到了Bean的问题。我决定为那些在处理文件列表相关解决方案时正在寻找解决方案的人创建这个问题。
英文:
Change the method signature to this:
@RequestMapping(value = "/mapUploads/submit", method = RequestMethod.POST)
protected void check(HttpServletRequest request) {
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
List<MultipartFile> files = multipartRequest.getFiles("files");
}
This problem was solved here. But in that question, OP was only using a single File in his parameter - and this is actually solvable by just adding MultipartFile
in the parameter signature:
@RequestMapping(value = "/mapUploads/submit", method = RequestMethod.POST)
protected void check(HttpServletRequest request, MultipartFile>files)
This was my previous circumstance, but I ran into the bean problem when I decided to accept a list of files. I decided to create this question for those who are searching for solutions with related to a list of files
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论