Search in sources :

Example 1 with MultipartFile

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;
}
Also used : MultipartFile(cn.taketoday.web.multipart.MultipartFile) DataSize(cn.taketoday.util.DataSize) FileSizeExceededException(cn.taketoday.http.FileSizeExceededException)

Example 2 with MultipartFile

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);
        }
    }
}
Also used : MultipartFile(cn.taketoday.web.multipart.MultipartFile) List(java.util.List) Map(java.util.Map)

Example 3 with MultipartFile

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);
}
Also used : MockMultipartFile(cn.taketoday.mock.web.MockMultipartFile) MultipartFile(cn.taketoday.web.multipart.MultipartFile) MockMultipartFile(cn.taketoday.mock.web.MockMultipartFile) Test(org.junit.jupiter.api.Test)

Example 4 with MultipartFile

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();
}
Also used : MockMultipartFile(cn.taketoday.mock.web.MockMultipartFile) MultipartFile(cn.taketoday.web.multipart.MultipartFile) MockMultipartFile(cn.taketoday.mock.web.MockMultipartFile) Test(org.junit.jupiter.api.Test)

Example 5 with MultipartFile

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);
}
Also used : File(java.io.File) MultipartFile(cn.taketoday.web.multipart.MultipartFile) POST(cn.taketoday.web.annotation.POST)

Aggregations

MultipartFile (cn.taketoday.web.multipart.MultipartFile)31 Test (org.junit.jupiter.api.Test)17 ResolvableMethodParameter (cn.taketoday.web.handler.method.ResolvableMethodParameter)11 MockMultipartFile (cn.taketoday.web.testfixture.servlet.MockMultipartFile)11 ServletRequestContext (cn.taketoday.web.servlet.ServletRequestContext)10 MockMultipartHttpServletRequest (cn.taketoday.web.testfixture.servlet.MockMultipartHttpServletRequest)10 BindingContext (cn.taketoday.web.BindingContext)8 RequestParam (cn.taketoday.web.annotation.RequestParam)7 MockMultipartFile (cn.taketoday.mock.web.MockMultipartFile)6 List (java.util.List)6 Part (jakarta.servlet.http.Part)5 LinkedMultiValueMap (cn.taketoday.core.LinkedMultiValueMap)4 Map (java.util.Map)4 PropertyValues (cn.taketoday.beans.PropertyValues)3 MultiValueMap (cn.taketoday.core.MultiValueMap)2 ContentDisposition (cn.taketoday.http.ContentDisposition)2 MultipartException (cn.taketoday.web.bind.MultipartException)2 NotMultipartRequestException (cn.taketoday.web.bind.NotMultipartRequestException)2 MaxUploadSizeExceededException (cn.taketoday.web.multipart.MaxUploadSizeExceededException)2 MultipartRequest (cn.taketoday.web.multipart.MultipartRequest)2