use of org.springframework.web.multipart.MultipartFile in project spring-framework by spring-projects.
the class RequestParamMethodArgumentResolverTests method resolveMultipartFile.
@Test
public void resolveMultipartFile() throws Exception {
MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
MultipartFile expected = new MockMultipartFile("mfile", "Hello World".getBytes());
request.addFile(expected);
webRequest = new ServletWebRequest(request);
MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(MultipartFile.class);
Object result = resolver.resolveArgument(param, null, webRequest, null);
assertTrue(result instanceof MultipartFile);
assertEquals("Invalid result", expected, result);
}
use of org.springframework.web.multipart.MultipartFile in project spring-framework by spring-projects.
the class RequestParamMethodArgumentResolverTests method supportsParameter.
@Test
public void supportsParameter() {
resolver = new RequestParamMethodArgumentResolver(null, true);
MethodParameter param = this.testMethod.annot(requestParam().notRequired("bar")).arg(String.class);
assertTrue(resolver.supportsParameter(param));
param = this.testMethod.annotPresent(RequestParam.class).arg(String[].class);
assertTrue(resolver.supportsParameter(param));
param = this.testMethod.annot(requestParam().name("name")).arg(Map.class);
assertTrue(resolver.supportsParameter(param));
param = this.testMethod.annotPresent(RequestParam.class).arg(MultipartFile.class);
assertTrue(resolver.supportsParameter(param));
param = this.testMethod.annotPresent(RequestParam.class).arg(List.class, MultipartFile.class);
assertTrue(resolver.supportsParameter(param));
param = this.testMethod.annotPresent(RequestParam.class).arg(MultipartFile[].class);
assertTrue(resolver.supportsParameter(param));
param = this.testMethod.annotPresent(RequestParam.class).arg(Part.class);
assertTrue(resolver.supportsParameter(param));
param = this.testMethod.annotPresent(RequestParam.class).arg(List.class, Part.class);
assertTrue(resolver.supportsParameter(param));
param = this.testMethod.annotPresent(RequestParam.class).arg(Part[].class);
assertTrue(resolver.supportsParameter(param));
param = this.testMethod.annot(requestParam().noName()).arg(Map.class);
assertFalse(resolver.supportsParameter(param));
param = this.testMethod.annotNotPresent(RequestParam.class).arg(String.class);
assertTrue(resolver.supportsParameter(param));
param = this.testMethod.annotNotPresent().arg(MultipartFile.class);
assertTrue(resolver.supportsParameter(param));
param = this.testMethod.annotNotPresent(RequestParam.class).arg(List.class, MultipartFile.class);
assertTrue(resolver.supportsParameter(param));
param = this.testMethod.annotNotPresent(RequestParam.class).arg(Part.class);
assertTrue(resolver.supportsParameter(param));
param = this.testMethod.annot(requestPart()).arg(MultipartFile.class);
assertFalse(resolver.supportsParameter(param));
param = this.testMethod.annot(requestParam()).arg(String.class);
assertTrue(resolver.supportsParameter(param));
param = this.testMethod.annot(requestParam().notRequired()).arg(String.class);
assertTrue(resolver.supportsParameter(param));
param = this.testMethod.annotPresent(RequestParam.class).arg(Optional.class, Integer.class);
assertTrue(resolver.supportsParameter(param));
param = this.testMethod.annotPresent(RequestParam.class).arg(Optional.class, MultipartFile.class);
assertTrue(resolver.supportsParameter(param));
resolver = new RequestParamMethodArgumentResolver(null, false);
param = this.testMethod.annotNotPresent(RequestParam.class).arg(String.class);
assertFalse(resolver.supportsParameter(param));
param = this.testMethod.annotPresent(RequestPart.class).arg(MultipartFile.class);
assertFalse(resolver.supportsParameter(param));
}
use of org.springframework.web.multipart.MultipartFile in project spring-framework by spring-projects.
the class RequestPartServletServerHttpRequestTests method getBodyWithWrappedRequest.
// SPR-13317
@Test
public void getBodyWithWrappedRequest() throws Exception {
byte[] bytes = "content".getBytes("UTF-8");
MultipartFile part = new MockMultipartFile("part", "", "application/json", bytes);
this.mockRequest.addFile(part);
HttpServletRequest wrapped = new HttpServletRequestWrapper(this.mockRequest);
ServerHttpRequest request = new RequestPartServletServerHttpRequest(wrapped, "part");
byte[] result = FileCopyUtils.copyToByteArray(request.getBody());
assertArrayEquals(bytes, result);
}
use of org.springframework.web.multipart.MultipartFile in project spring-framework by spring-projects.
the class RequestPartMethodArgumentResolverTests method resolveOptionalMultipartFileList.
@Test
public void resolveOptionalMultipartFileList() throws Exception {
MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
MultipartFile expected = new MockMultipartFile("requestPart", "Hello World".getBytes());
request.addFile(expected);
request.addFile(new MockMultipartFile("otherPart", "", "text/plain", "Hello World".getBytes()));
webRequest = new ServletWebRequest(request);
Object actualValue = resolver.resolveArgument(optionalMultipartFileList, null, webRequest, null);
assertTrue(actualValue instanceof Optional);
assertEquals("Invalid result", Collections.singletonList(expected), ((Optional) actualValue).get());
actualValue = resolver.resolveArgument(optionalMultipartFileList, null, webRequest, null);
assertTrue(actualValue instanceof Optional);
assertEquals("Invalid result", Collections.singletonList(expected), ((Optional) actualValue).get());
}
use of org.springframework.web.multipart.MultipartFile in project disconf by knightliao.
the class FileUploadValidator method validateFiles.
/**
* 验证文件大小,文件名,文件后缀
*
* @param files
* @param maxLength
* @param allowExtName
*
* @return
*/
public List<MultipartFile> validateFiles(List<MultipartFile> files, long maxLength, String[] allowExtName) {
List<MultipartFile> retFiles = new ArrayList<MultipartFile>();
for (MultipartFile multipartFile : files) {
try {
this.validateFile(multipartFile, maxLength, allowExtName);
retFiles.add(multipartFile);
} catch (Exception e) {
LOG.warn(e.toString());
}
}
return retFiles;
}
Aggregations