2015-09-05 47 views
8

我嘗試實現加載照片和String對象。這是我的方法的聲明。無法處理部件,因爲即使現有的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 

回答

0

我總是包裹多文件與其他性質的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 
} 
-1

嘗試添加該塊代碼添加到您的配置:

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

並在web.xml上加載你的配置

+0

成爲一個答案,這至少錯過了一些行:) – planetmaker

相關問題