本文介绍了无法将字符串转换为MultipartFile的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮助我解决问题!我有引导文件输入,包含文件:

 < link href ="/assets/css/fileinput/fileinput.css" rel ="stylesheet" type ="text/css">< link href ="/assets/css/fileinput/fileinput-rtl.css" rel ="stylesheet" type ="text/css">< script type ="text/javascript" src ="/assets/js/fileinput/fileinput.js"></script>< script type ="text/javascript" src ="/assets/js/plugins/fileinput/piexif.js"></script>< script type ="text/javascript" src ="/assets/js/plugins/fileinput/sortable.js"></script>< script type ="text/javascript" src ="/assets/js/plugins/fileinput/purify.js"></script> 

我还有一个用于fileInput的宏:

 < #macro fileInputBind所需的路径= false fileMask =">< @ spring.bind路径/>< div class ="form-group">< @所需的标签路径/>< #assign namedPath = path?replace(.",-")/>< #assign fileInputId ="$ {replacedPath}"/>< div class ="col-md-12">< input id ="$ {fileInputId}" name ="$ {spring.status.expression}" type ="file" class ="form-control file-styled"accept ="$ {fileMask}"></div></div></#macro> 

这是我的ftl元素:

 < @ form.fileInputBind"incidentRovdCreateForm.attachment"/> 

然后在我的表单中使用变量来处理此字段:

 私有MultipartFile附件; 

但是当执行@PostMapping时,这部分出现了错误:

  if(bindingResult.hasErrors())返回新的ModelAndView("incident-rovd/create").addObject("typeIncident1",eventTypeLevel1Repository.findAll()).addObject("typeIncident2",form.getIncidentTypeLevel1()== null?Collections.emptyList():eventingTypeLevel2Repository.findAllByParent(form.getIncidentTypeLevel1())).addObject("typeIncident3",form.getIncidentTypeLevel2()== null?Collections.emptyList():eventingTypeLevel3Repository.findAllByParent(form.getIncidentTypeLevel2())).addObject("incidentRovdCreateForm",form); 

错误:无法将类型"java.lang.String"的属性值转换为属性"attachment"的必需类型"org.springframework.web.multipart.MultipartFile";嵌套异常为java.lang.IllegalStateException:无法将属性附件"的类型"java.lang.String"的值转换为所需的类型"org.springframework.web.multipart.MultipartFile":找不到匹配的编辑器或转换策略./p>

我可以遇到什么问题?

解决方案

我在HTML + JavaScript中遇到了与前端相同的问题.如果不需要bean的file属性,则以下内容可以为您提供帮助:

 //jsvar formData = new FormData(document.getElementById("yourFormId")));//TestBean.java@短暂的私有MultipartFile文件; 

此方法避免了无法将字符串转换为MultipartFile".错误.

Please help me to resolve my problem!I have bootstrap fileinput, files are included:

    <link href="/assets/css/fileinput/fileinput.css" rel="stylesheet" type="text/css">
<link href="/assets/css/fileinput/fileinput-rtl.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="/assets/js/fileinput/fileinput.js"></script>
<script type="text/javascript" src="/assets/js/plugins/fileinput/piexif.js"></script>
<script type="text/javascript" src="/assets/js/plugins/fileinput/sortable.js"></script>
<script type="text/javascript" src="/assets/js/plugins/fileinput/purify.js"></script>

Also I have a macros for fileInput:

<#macro fileInputBind path required=false fileMask="">
    <@spring.bind path/>
<div class="form-group">
    <@label path required/>
    <#assign replacedPath = path?replace(".", "-") />
    <#assign fileInputId = "${replacedPath}" />
    <div class="col-md-12">
        <input id="${fileInputId}" name="${spring.status.expression}" type="file" class="form-control file-styled"
               accept="${fileMask}">
    </div>
</div>
</#macro>

Here is my ftl element:

<@form.fileInputBind "incidentRovdCreateForm.attachment"/>

And variable in my form to handle this field:

private MultipartFile attachment;

But when @PostMapping executed I got an error in this part:

if (bindingResult.hasErrors())
            return new ModelAndView("incident-rovd/create")
                    .addObject("typeIncident1", incidentTypeLevel1Repository.findAll())
                    .addObject("typeIncident2", form.getIncidentTypeLevel1() == null ? Collections.emptyList() : incidentTypeLevel2Repository.findAllByParent(form.getIncidentTypeLevel1()))
                    .addObject("typeIncident3", form.getIncidentTypeLevel2() == null ? Collections.emptyList() : incidentTypeLevel3Repository.findAllByParent(form.getIncidentTypeLevel2()))
                    .addObject("incidentRovdCreateForm", form);

Error:Failed to convert property value of type 'java.lang.String' to required type 'org.springframework.web.multipart.MultipartFile' for property 'attachment'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'org.springframework.web.multipart.MultipartFile' for property 'attachment': no matching editors or conversion strategy found.

What problem can i have?

解决方案

I have encountered the same problem with HTML + JavaScript as the frontend. If the file property of bean is not required, following may help you:

// js
var formData = new FormData(document.getElementById("yourFormId"));

//TestBean.java
@Transient
private MultipartFile file;

This method avoids the "Cannot convert String to MultipartFile" error.

这篇关于无法将字符串转换为MultipartFile的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-08 21:40