java 无法将“org.springframework.web.multipart.commons.CommonsMultipartFile”类型的值转换为所需类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30005724/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Failed to convert value of type 'org.springframework.web.multipart.commons.CommonsMultipartFile' to required type
提问by gstackoverflow
I have following controller method:
我有以下控制器方法:
@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 source:
OnlyForImagesFileWrapper 来源:
public class OnlyForImagesFileWrapper {
@Extensions(imageFormats = {".jpg",".png",".gif",".bmp"}, videoFormats = {})
private MultipartFile multipartFile;
...
}
How to avoid the problem?
如何避免问题?
Where can I set conversion politic for this controller method for multipart file?
我在哪里可以为多部分文件的这个控制器方法设置转换策略?
P.S.
聚苯乙烯
I tryed to write my custom initbinder:
我尝试编写我的自定义 initbinder:
@InitBinder
public void initBinder(WebDataBinder binder) {
binder.registerCustomEditor(CommonsMultipartFile.class, new PropertyEditorSupport() {
@Override
public void setValue(Object file) {
setValue(new OnlyForImagesFileWrapper((MultipartFile) file));
}
});
}
But this method doesn't invoke when I submit form and I see stacktrace mentioned above.
但是当我提交表单并且我看到上面提到的堆栈跟踪时,此方法不会调用。
P.S.
聚苯乙烯
result after M. Deinuminstruction execution(when I inside saveTerminal
method):
M. Deinum指令执行后的结果(当我在saveTerminal
方法中时):
Also I noticed that my initbinder method doesn't invoke.
我还注意到我的 initbinder 方法没有调用。
More details about my code(state after M. Denium advice):
有关我的代码的更多详细信息(在 M. Denium 建议之后声明):
jsp:
jsp:
<input type="file" id="newFile" name="file" class="file" size="21.5" accept=".jpg,.png,.gif,.bmp" style="opacity: 0;">
arguments of controller method:
控制器方法的参数:
...
@ModelAttribute @Valid OnlyForImagesFileWrapper wrapper,
BindingResult bindingResult,
...
采纳答案by M. Deinum
As I commented you are making things too complex. Change your wrapper to the following (with the appropriate getters and setters).
正如我评论的那样,你让事情变得太复杂了。将您的包装器更改为以下内容(使用适当的 getter 和 setter)。
public class OnlyForImagesFileWrapper {
@Extensions(imageFormats = {".jpg",".png",".gif",".bmp"}, videoFormats = {})
private MultipartFile file;
private String name;
...
}
Then your controller method
然后你的控制器方法
@RequestMapping(value = "/owner/terminals/save", method = RequestMethod.POST)
public String saveTerminal( @ModelAttribute @Valid OnlyForImagesFileWrapper wrapper, BindingResult bindingResult ) { ... }
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.
当然,在你的配置中,确保你有一个MultipartFileResolver
配置来正确处理参考指南中MultipartFile
解释的参数。