From e0d2e20fc4e63baccca34a1348bc8e8918fe16ac Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Thu, 30 Jun 2011 21:04:23 +0000 Subject: [PATCH] 8483 Add support for MultipartFile arg type in RequestPartMethodArgumentResolver --- .../RequestPartMethodArgumentResolver.java | 6 +++++- .../RequestPartMethodArgumentResolverTests.java | 16 ++++++++++++++-- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/method/annotation/support/RequestPartMethodArgumentResolver.java b/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/method/annotation/support/RequestPartMethodArgumentResolver.java index f3f85fcf7a..f8db5a9722 100644 --- a/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/method/annotation/support/RequestPartMethodArgumentResolver.java +++ b/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/method/annotation/support/RequestPartMethodArgumentResolver.java @@ -32,6 +32,7 @@ import org.springframework.web.bind.annotation.RequestPart; import org.springframework.web.bind.support.WebDataBinderFactory; import org.springframework.web.context.request.NativeWebRequest; import org.springframework.web.method.support.ModelAndViewContainer; +import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartHttpServletRequest; import org.springframework.web.multipart.MultipartRequest; import org.springframework.web.multipart.RequestPartServletServerHttpRequest; @@ -77,8 +78,11 @@ public class RequestPartMethodArgumentResolver extends AbstractMessageConverterM } String partName = getPartName(parameter); + if (MultipartFile.class.isAssignableFrom(parameter.getParameterType())) { + return multipartRequest.getFile(partName); + } + HttpInputMessage inputMessage = new RequestPartServletServerHttpRequest(multipartRequest, partName); - Object arg = readWithMessageConverters(inputMessage, parameter, parameter.getParameterType()); if (isValidationApplicable(arg, parameter)) { diff --git a/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/method/annotation/support/RequestPartMethodArgumentResolverTests.java b/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/method/annotation/support/RequestPartMethodArgumentResolverTests.java index 6fa132a461..0a36662303 100644 --- a/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/method/annotation/support/RequestPartMethodArgumentResolverTests.java +++ b/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/method/annotation/support/RequestPartMethodArgumentResolverTests.java @@ -23,6 +23,7 @@ import static org.easymock.EasyMock.isA; import static org.easymock.EasyMock.replay; import static org.easymock.EasyMock.reset; import static org.easymock.EasyMock.verify; +import static org.junit.Assert.*; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; @@ -71,6 +72,7 @@ public class RequestPartMethodArgumentResolverTests { private MethodParameter paramRequestPart; private MethodParameter paramNamedRequestPart; private MethodParameter paramValidRequestPart; + private MethodParameter paramMultipartFile; private MethodParameter paramInt; private NativeWebRequest webRequest; @@ -82,12 +84,14 @@ public class RequestPartMethodArgumentResolverTests { @SuppressWarnings("unchecked") @Before public void setUp() throws Exception { - Method handle = getClass().getMethod("handle", SimpleBean.class, SimpleBean.class, SimpleBean.class, Integer.TYPE); + Method handle = getClass().getMethod("handle", + SimpleBean.class, SimpleBean.class, SimpleBean.class, MultipartFile.class, Integer.TYPE); paramRequestPart = new MethodParameter(handle, 0); paramRequestPart.initParameterNameDiscovery(new LocalVariableTableParameterNameDiscoverer()); paramNamedRequestPart = new MethodParameter(handle, 1); paramValidRequestPart = new MethodParameter(handle, 2); - paramInt = new MethodParameter(handle, 3); + paramMultipartFile = new MethodParameter(handle, 3); + paramInt = new MethodParameter(handle, 4); messageConverter = createMock(HttpMessageConverter.class); expect(messageConverter.getSupportedMediaTypes()).andReturn(Collections.singletonList(MediaType.TEXT_PLAIN)); @@ -119,6 +123,13 @@ public class RequestPartMethodArgumentResolverTests { testResolveArgument(new SimpleBean("foo"), paramNamedRequestPart); } + @Test + public void resolveMultipartFile() throws Exception { + Object actual = resolver.resolveArgument(paramMultipartFile, null, webRequest, null); + assertNotNull(actual); + assertSame(multipartFile, actual); + } + @Test public void resolveRequestPartNotValid() throws Exception { try { @@ -156,6 +167,7 @@ public class RequestPartMethodArgumentResolverTests { public void handle(@RequestPart SimpleBean requestPart, @RequestPart("requestPart") SimpleBean namedRequestPart, @Valid @RequestPart("requestPart") SimpleBean validRequestPart, + @RequestPart("requestPart") MultipartFile multipartFile, int i) { }