无法将类型"org.springframework.web.multipart.commons.CommonsMultipartFile"的值转换为所需类型 [英] Failed to convert value of type 'org.springframework.web.multipart.commons.CommonsMultipartFile' to required type

查看:1444
本文介绍了无法将类型"org.springframework.web.multipart.commons.CommonsMultipartFile"的值转换为所需类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下控制器方法:

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

,我看到以下堆栈跟踪:

and I see the following 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

OnlyForImagesFileWrapper来源:

OnlyForImagesFileWrapper source:

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

如何避免该问题?

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

Where can I set conversion politic for this controller method for multipart file?

PS

我试图编写自定义的initbinder:

I tryed to write my custom 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时,不会调用此方法.

But this method doesn't invoke when I submit form and I see stacktrace mentioned above.

PS

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

result after M. Deinum instruction execution(when I inside saveTerminal method):

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

Also I noticed that my initbinder method doesn't invoke.

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

More details about my code(state after M. Denium advice):

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,
...

推荐答案

正如我评论的那样,您使事情变得太复杂了.将包装程序更改为以下内容(使用适当的getter和setter方法).

As I commented you are making things too complex. Change your wrapper to the following (with the appropriate getters and setters).

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参数,如

And of course in your configuration make sure you have a MultipartFileResolver configured to properly handle the MultipartFile argument as explained in the reference guide.

这篇关于无法将类型"org.springframework.web.multipart.commons.CommonsMultipartFile"的值转换为所需类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆