use of cn.taketoday.web.multipart.MultipartFile in project today-infrastructure by TAKETODAY.
the class AbstractMultipartResolver method resolveInternal.
/**
* @throws FileSizeExceededException upload file size exceeded
* @see MultipartConfig#getMaxRequestSize()
*/
@Override
protected Object resolveInternal(RequestContext context, ResolvableMethodParameter parameter) throws Throwable {
if (context.isMultipart()) {
DataSize maxRequestSize = getMultipartConfig().getMaxRequestSize();
// exceed max size?
if (maxRequestSize.toBytes() < context.getContentLength()) {
throw new FileSizeExceededException(maxRequestSize, null, DataSize.ofBytes(context.getContentLength()));
}
MultiValueMap<String, MultipartFile> multipartFiles = context.getMultipartRequest().getMultipartFiles();
return resolveInternal(context, parameter, multipartFiles);
}
return null;
}
use of cn.taketoday.web.multipart.MultipartFile in project today-infrastructure by TAKETODAY.
the class WebDataBinder method bindMultipart.
/**
* Bind all multipart files contained in the given request, if any
* (in case of a multipart request). To be called by subclasses.
* <p>Multipart files will only be added to the property values if they
* are not empty or if we're configured to bind empty multipart files too.
*
* @param multipartFiles a Map of field name String to MultipartFile object
* @param mpvs the property values to be bound (can be modified)
* @see cn.taketoday.web.multipart.MultipartFile
* @see #setBindEmptyMultipartFiles
*/
protected void bindMultipart(Map<String, List<MultipartFile>> multipartFiles, PropertyValues mpvs) {
for (Map.Entry<String, List<MultipartFile>> entry : multipartFiles.entrySet()) {
List<MultipartFile> values = entry.getValue();
String key = entry.getKey();
if (values.size() == 1) {
MultipartFile value = values.get(0);
if (isBindEmptyMultipartFiles() || !value.isEmpty()) {
mpvs.add(key, value);
}
} else {
mpvs.add(key, values);
}
}
}
use of cn.taketoday.web.multipart.MultipartFile in project today-infrastructure by TAKETODAY.
the class MultipartRequestMatchersTests method testByteArrayNoMatch.
@Test
public void testByteArrayNoMatch() throws Exception {
MultipartFile f1 = new MockMultipartFile("f1", "foo.txt", "text/plain", "Foo Lorem ipsum".getBytes());
MultipartFile f2 = new MockMultipartFile("f2", "bar.txt", "text/plain", "Bar Lorem ipsum".getBytes());
this.input.add("fooParam", "foo value");
this.input.add("barParam", "bar value");
this.input.add(f1.getName(), f1.getResource());
this.input.add(f2.getName(), f2.getResource());
this.expected.addAll(this.input);
this.expected.set(f1.getName(), f2.getBytes());
assertThatExceptionOfType(AssertionError.class).isThrownBy(this::writeAndAssert);
}
use of cn.taketoday.web.multipart.MultipartFile in project today-infrastructure by TAKETODAY.
the class MultipartRequestMatchersTests method testByteArrayMatch.
@Test
public void testByteArrayMatch() throws Exception {
MultipartFile f1 = new MockMultipartFile("f1", "foo.txt", "text/plain", "Foo Lorem ipsum".getBytes());
MultipartFile f2 = new MockMultipartFile("f2", "bar.txt", "text/plain", "Bar Lorem ipsum".getBytes());
MultipartFile f3 = new MockMultipartFile("f3", "foobar.txt", "text/plain", "Foobar Lorem ipsum".getBytes());
this.input.add("fooParam", "foo value");
this.input.add("barParam", "bar value");
this.input.add(f1.getName(), f1.getResource());
this.input.add(f2.getName(), f2.getResource());
this.input.add(f3.getName(), f3.getResource());
this.expected.addAll(this.input);
this.expected.set(f1.getName(), f1.getBytes());
this.expected.set(f2.getName(), f2.getBytes());
this.expected.set(f3.getName(), f3.getBytes());
writeAndAssert();
}
use of cn.taketoday.web.multipart.MultipartFile in project today-framework by TAKETODAY.
the class LightWebApplication method upload.
@POST("/upload")
public UploadResult upload(MultipartFile file, String other) throws IOException {
final String fileName = file.getFileName();
final long size = file.getSize();
final String content = new String(file.getBytes());
final String name = file.getName();
final File file1 = new File("D:/dev/temp/upload", fileName);
file.save(file1);
return new UploadResult(fileName, size, content, name, other);
}
Aggregations