Java 无法转换类型为';org.springframework.web.multipart.commons.commons多部分文件';到所需类型

Java 无法转换类型为';org.springframework.web.multipart.commons.commons多部分文件';到所需类型,java,spring,spring-mvc,spring-mvc-initbinders,Java,Spring,Spring Mvc,Spring Mvc Initbinders,我有以下控制器方法: @RequestMapping(value = "/owner/terminals/save", method = RequestMethod.POST) public String saveTerminal( @RequestParam(value = "name") String name, @RequestParam(value = "file") @Valid OnlyForImagesFileWrapper

我有以下控制器方法:

@RequestMapping(value = "/owner/terminals/save", method = RequestMethod.POST)
public String saveTerminal( @RequestParam(value = "name") String name, 
                            @RequestParam(value = "file") @Valid OnlyForImagesFileWrapper file,
                               BindingResult bindingResult )
             {
                ...
...
@ModelAttribute @Valid OnlyForImagesFileWrapper wrapper,
                               BindingResult bindingResult,
...
我看到了下面的stacktrace:

    org.springframework.beans.ConversionNotSupportedException: Failed to convert value of type 'org.springframework.web.multipart.commons.CommonsMultipartFile' to required type 'com.terminal.domain.validation.OnlyForImagesFileWrapper'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [org.springframework.web.multipart.commons.CommonsMultipartFile] to required type [com.terminal.domain.validation.OnlyForImagesFileWrapper]: no matching editors or conversion strategy found
        at           org.springframework.beans.TypeConverterSupport.doConvert(TypeConverterSupport.java:74)
        ....
        Caused by: java.lang.IllegalStateException: Cannot convert value of type [org.springframework.web.multipart.commons.CommonsMultipartFile] to required type [com.terminal.domain.validation.OnlyForImagesFileWrapper]: no matching editors or conversion strategy found
            at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:267)
            ... 71 more
仅适用于图像文件包装器源:

public class OnlyForImagesFileWrapper {
    @Extensions(imageFormats = {".jpg",".png",".gif",".bmp"}, videoFormats = {})
    private MultipartFile multipartFile;
    ...
}
如何避免这个问题

在哪里可以为多部分文件的此控制器方法设置转换策略

p.S.

我试着写我的自定义initbinder:

@InitBinder
    public void initBinder(WebDataBinder binder) {
        binder.registerCustomEditor(CommonsMultipartFile.class, new PropertyEditorSupport() {
            @Override
            public void setValue(Object file) {
                setValue(new OnlyForImagesFileWrapper((MultipartFile) file));
            }
        });
    }
但当我提交表单时,这个方法并没有调用,我看到上面提到的stacktrace

p.S.

执行M.Deinum指令后的结果(当我在
saveTerminal
方法中时):

我还注意到我的initbinder方法没有调用

有关我的代码的更多详细信息(在M.Denium建议后陈述):

jsp:


正如我所评论的,你把事情弄得太复杂了。将包装器更改为以下内容(使用适当的getter和setter)

然后是控制器方法

@RequestMapping(value = "/owner/terminals/save", method = RequestMethod.POST)
public String saveTerminal( @ModelAttribute @Valid OnlyForImagesFileWrapper wrapper, BindingResult bindingResult ) { ... }

当然,在您的配置中,请确保配置了
MultipartFileResolver
,以正确处理
MultipartFile
参数,如中所述。

您是否阅读了。。。如果您想要绑定,也可以使用绑定。添加类型为
String
的属性
name
,并将
multipartFile
重命名为
file
。从方法中远程
@RequestParam
s,只需使用
@modeldattribute
注释键入的方法参数
OnlyForImagesFileWrapper
。您正在混合和混淆许多东西,这使事情变得更加复杂。@M.Deinum在参考quid中写道:我如何接受MultipartFile作为方法属性。在我的情况下,我想接受包装纸MultipartFile@M.Deinum添加一个字符串类型的属性名,并将multipartFile重命名为file where?执行指令后,saveTerminal始终接受null参数,但您没有执行我告诉您的操作。您尚未将属性从
multipartFile
重命名为
file
。您还应该删除init binder方法,因为它没有任何作用。您能解释一下为什么我应该将multipartFile重命名为file吗?文件名是什么?看起来像是来自jspRequest的名称参数绑定到相同名称的属性。因此,如果您有一个请求参数
file
,您应该在模型对象上有一个
setFile
方法。(内部字段的调用方式并不重要,getter/setter的名称定义了属性的名称)。
public class OnlyForImagesFileWrapper {
    @Extensions(imageFormats = {".jpg",".png",".gif",".bmp"}, videoFormats = {})
    private MultipartFile file;
    private String name;
...
}
@RequestMapping(value = "/owner/terminals/save", method = RequestMethod.POST)
public String saveTerminal( @ModelAttribute @Valid OnlyForImagesFileWrapper wrapper, BindingResult bindingResult ) { ... }