【问题标题】:Failed to convert value of type 'org.springframework.web.multipart.commons.CommonsMultipartFile' to required type无法将“org.springframework.web.multipart.commons.CommonsMultipartFile”类型的值转换为所需类型
【发布时间】:2015-05-02 18:07:17
【问题描述】:

我有以下控制器方法:

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

我看到以下堆栈跟踪:

    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

OnlyForImagesFileWrapper 来源:

public class OnlyForImagesFileWrapper {
    @Extensions(imageFormats = {".jpg",".png",".gif",".bmp"}, videoFormats = {})
    private MultipartFile multipartFile;
    ...
}

如何避免问题?

我在哪里可以为这个控制器方法为多部分文件设置转换策略?

附言

我尝试编写我的自定义 initbinder:

@InitBinder
    public void initBinder(WebDataBinder binder) {
        binder.registerCustomEditor(CommonsMultipartFile.class, new PropertyEditorSupport() {
            @Override
            public void setValue(Object file) {
                setValue(new OnlyForImagesFileWrapper((MultipartFile) file));
            }
        });
    }

但是当我提交表单并且我看到上面提到的堆栈跟踪时,这个方法没有调用。

附言

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

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

关于我的代码的更多细节(M. Denium 建议之后的状态):

jsp:

<input type="file" id="newFile" name="file" class="file" size="21.5" accept=".jpg,.png,.gif,.bmp" style="opacity: 0;">

控制器方法的参数:

...
@ModelAttribute @Valid OnlyForImagesFileWrapper wrapper,
                               BindingResult bindingResult,
...

【问题讨论】:

  • 你读过reference guide...另外,如果你想绑定然后使用绑定。添加String 类型的属性name 并将multipartFile 重命名为file。从方法中远程@RequestParams,只需用@ModelAttribute 注释键入OnlyForImagesFileWrapper 的方法参数。你混合和混淆了很多东西,这让事情变得比需要的复杂得多。
  • @M.参考 quid 中的 Deinum 编写了如何接受 MultipartFile 作为方法属性。就我而言,我想接受 MultipartFile 的包装器
  • @M. Deinum 添加一个String类型的属性名并将multipartFile重命名为file where?

标签: java spring spring-mvc spring-mvc-initbinders


【解决方案1】:

正如我评论的那样,你让事情变得太复杂了。将您的包装器更改为以下内容(使用适当的 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 ) { ... }

当然,在您的配置中,请确保您已将 MultipartFileResolver 配置为正确处理 MultipartFile 参数,如 the reference guide 中所述。

【讨论】:

  • 执行你的指令后 saveTerminal 总是接受空参数dl2.joxi.net/drive/0005/3037/338909/150502/e592df25e4.jpg
  • 你没有按照我说的去做。您尚未将属性从 multipartFile 重命名为 file。此外,您应该删除您的 init binder 方法,因为它没有做任何事情。
  • 你能解释一下为什么我应该将 multipartFile 重命名为 file 吗?文件名是什么?
  • 貌似是jsp的名字
  • 请求参数绑定同名属性。因此,如果您有一个请求参数file,您的模型对象上应该有一个setFile 方法。 (如何调用内部字段并不重要,getter/setter 的名称定义了属性的名称)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-09-02
  • 2016-07-03
  • 2017-05-20
  • 2020-07-02
  • 2017-01-28
  • 2019-08-23
  • 2019-11-05
相关资源
最近更新 更多