Spring not accepting list of multipart files: java.lang.NoSuchMethodException: org.springframework.web.multipart.MultipartFile

huangapple go评论78阅读模式
英文:

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(&#39;files&#39;).files.length;
for (var index = 0; index &lt; totalfiles; index++) {
    formData.append(&quot;files&quot;, document.getElementById(&#39;files&#39;).files[index]);
}

And with my Spring 4 application, it should have been received by this method:

@RequestMapping(value = &quot;/mapUploads/submit&quot;, method = RequestMethod.POST)
protected void check(HttpServletRequest request, List&lt;MultipartFile&gt; 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 = &quot;/mapUploads/submit&quot;, method = RequestMethod.POST)
protected void check(HttpServletRequest request) {
   MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
   List&lt;MultipartFile&gt; files = multipartRequest.getFiles(&quot;files&quot;);
}

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 = &quot;/mapUploads/submit&quot;, method = RequestMethod.POST)
protected void check(HttpServletRequest request, MultipartFile&gt;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

huangapple
  • 本文由 发表于 2020年9月3日 13:49:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/63717596.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定