java.lang.IllegalArgumentException:预期的MultipartHttpServletRequest:是否配置了MultipartResolver?

时间:2014-11-16 15:03:45

标签: java spring-mvc

我正在尝试上传.jpg文件,但收到此消息:

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalArgumentException: Expected MultipartHttpServletRequest: is a MultipartResolver configured?
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:978)
    org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:868)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:644)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
    org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:118)
    org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:84)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:103)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:154)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:199)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:110)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:57)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:50)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192)
    org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160)
    org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:344)
    org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:261)

但我有一个多部分:

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

现在我的问题是如何解决这个问题,这里有更多代码:

UploadController:

@Controller
@RequestMapping(value = "/upload")
public class UploadController {
private static final Logger LOGGER = LoggerFactory.getLogger(UploadController.class);

private String folderToUpload = "/resources/img";

@RequestMapping(value = "/form", method = RequestMethod.GET)
public ModelAndView formUpload(){
    ModelAndView mv = new ModelAndView("upload/form");

    return mv;
}

@RequestMapping(value = "/form", method = RequestMethod.POST)
public ModelAndView procesFormUpload(HttpSession session, @RequestParam("photofile") MultipartFile file) {
    ModelAndView mv = new ModelAndView("upload/list");

    String uploadLocation = Upload(session).getAbsolutePath();
    LOGGER.debug("Name File: [{}]", file.getOriginalFilename());
    LOGGER.debug("Size File: [{}]", file.getSize());
    LOGGER.debug("Type File: [{}]", file.getContentType());

    File photo = new File(uploadLocation + File.separator + file.getOriginalFilename());
    try {
        file.transferTo(photo);
        LOGGER.debug("File [{}]", photo.getAbsolutePath());
    } catch (Exception ex) {
        LOGGER.error("Kan file niet uploaden");
        LOGGER.error("stacktrace: {}", ex.getMessage());
    }
    return mv;
}
private File Upload(HttpSession session) {
    String locationFullPath = session.getServletContext().getRealPath(folderToUpload);
    LOGGER.debug("location full path: [{}]", locationFullPath);
    File test = new File(locationFullPath);

    if(!test.exists()){
        LOGGER.debug("wtf");
        test.mkdirs();
    }
    return test;
}
}

form.jsp:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="t" tagdir="/WEB-INF/tags" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="form" uri="http://www.springframework.org/tags/form"  %>
<%-- 
Document   : form
Created on : 16-nov-2014, 13:44:37
Author     : Nicky
--%>

<!DOCTYPE html>
<t:masterpage>
  <jsp:body>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
    <title>Form upload</title>

    <h1>Form upload</h1>

    <form method="POST" enctype="multipart/form-data">

        <table border="0">
            <tbody>
                <tr>
                    <td>File</td>
                    <td>

                        <input type="file" name="photofile">

                    </td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                    <td>

                        <input type="submit" value="Upload">

                    </td>
                </tr>
            </tbody>
        </table>

    </form>
</jsp:body>

我希望有人可以帮助我!

1 个答案:

答案 0 :(得分:2)

    @Configuration
public class FileConfig {

    @Bean(name = "multipartResolver")
    public CommonsMultipartResolver multipartResolver() {
        CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
        multipartResolver.setMaxUploadSize(10485760);
        return multipartResolver;
    }
}