【问题标题】:Unable to process parts as no multi-part configuration has been provided even while existing multipartResolver即使现有的 multipartResolver 也没有提供多部分配置,因此无法处理部分
【发布时间】:2015-09-05 14:13:50
【问题描述】:

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

@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

【问题讨论】:

    标签: java spring-mvc multipartform-data


    【解决方案1】:

    我总是将多部分文件包装在 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
    }
    

    【讨论】:

      【解决方案2】:

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

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

      并在 web.xml 上加载您的配置

      【讨论】:

      • 成为一个答案,这至少错过了一些行:)
      猜你喜欢
      • 2016-08-16
      • 2017-02-08
      • 2014-06-29
      • 2014-08-26
      • 2016-10-21
      • 2017-04-17
      • 2020-10-22
      • 2014-08-07
      • 1970-01-01
      相关资源
      最近更新 更多