无法处理零件,因为即使现有的 multipartResolver 也没有提供多部分配置

Unable to process parts as no multi-part configuration has been provided even while existing multipartResolver

我尝试实现加载照片和字符串对象。这是我的方法的声明。

@RequestMapping(method = RequestMethod.PUT, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public @ResponseBody ResponseEntity<UserWithPhoto> update(@RequestHeader(value="Access-key") String accessKey,
                                         @RequestHeader(value="Secret-key") String secretKey,
                                         @RequestPart("user") String string,
                                         @RequestPart("photo") MultipartFile file) throws Exception

这是我的多部分解析器

<beans:bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <beans:property name="maxUploadSize" value="10000000" />
 </beans:bean>

我不知道为什么我得到

java.lang.IllegalStateException: Unable to process parts as no multi-part configuration has been provided

尝试将此块代码添加到您的配置中:

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/index" />
        <property name="suffix" value=".jsp" />
</bean>

并在 web.xml

上加载您的配置

我总是将多部分文件包装在具有其他所需属性的 POJO 中:

public class FileUpload {

   private Long id;
   private MultipartFile file;

   // getters and setters
}

在我看来:

<spring:url value="/myEndpoint" var="url_upload"/>
<form:form method="POST" enctype="multipart/form-data" commandName="fileUpload" action="${url_upload}" >

    <form:hidden path="id" />
    <input type="file" name="file" id="inputFile"/>

    <input type="submit" value="Upload" />
</form:form>        

在端点中:

@RequestMapping(value = "/myEndpoint", method = RequestMethod.POST)
public String uploadFile(@ModelAttribute("fileUpload") FileUpload dto, Model uiModel) {
    // Process file
}