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

时间:2015-09-05 14:13:51

标签: java spring-mvc multipartform-data

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

@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

2 个答案:

答案 0 :(得分:0)

我总是将多部分文件包装在POJO中,并且需要其他属性:

<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
}

在端点中:

{{1}}

答案 1 :(得分:-1)

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

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

并在web.xml上加载配置