Search in sources :

Example 71 with MultipartFile

use of org.springframework.web.multipart.MultipartFile in project qpp-conversion-tool by CMSgov.

the class QrdaControllerV1Test method testExceptionalInputStreamSupplier.

@Test
void testExceptionalInputStreamSupplier() {
    Assertions.assertThrows(UncheckedIOException.class, () -> {
        MultipartFile spy = spy(multipartFile);
        try {
            doThrow(new IOException()).when(spy).getInputStream();
            InputStream in = objectUnderTest.inputStream(spy);
            in.close();
        } catch (IOException ex) {
            Assertions.fail("wrong exception");
        }
    });
}
Also used : MockMultipartFile(org.springframework.mock.web.MockMultipartFile) MultipartFile(org.springframework.web.multipart.MultipartFile) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) Test(org.junit.jupiter.api.Test)

Example 72 with MultipartFile

use of org.springframework.web.multipart.MultipartFile in project spring-framework by spring-projects.

the class RequestPartServletServerHttpRequest method getBody.

@Override
public InputStream getBody() throws IOException {
    // Prefer Servlet Part resolution to cover file as well as parameter streams
    boolean servletParts = (this.multipartRequest instanceof StandardMultipartHttpServletRequest);
    if (servletParts) {
        Part part = retrieveServletPart();
        if (part != null) {
            return part.getInputStream();
        }
    }
    // Spring-style distinction between MultipartFile and String parameters
    MultipartFile file = this.multipartRequest.getFile(this.requestPartName);
    if (file != null) {
        return file.getInputStream();
    }
    String paramValue = this.multipartRequest.getParameter(this.requestPartName);
    if (paramValue != null) {
        return new ByteArrayInputStream(paramValue.getBytes(determineCharset()));
    }
    // Fallback: Servlet Part resolution even if not indicated
    if (!servletParts) {
        Part part = retrieveServletPart();
        if (part != null) {
            return part.getInputStream();
        }
    }
    throw new IllegalStateException("No body available for request part '" + this.requestPartName + "'");
}
Also used : MultipartFile(org.springframework.web.multipart.MultipartFile) ByteArrayInputStream(java.io.ByteArrayInputStream) Part(jakarta.servlet.http.Part)

Example 73 with MultipartFile

use of org.springframework.web.multipart.MultipartFile in project spring-framework by spring-projects.

the class MultipartResolutionDelegate method resolveMultipartArgument.

@Nullable
public static Object resolveMultipartArgument(String name, MethodParameter parameter, HttpServletRequest request) throws Exception {
    MultipartHttpServletRequest multipartRequest = WebUtils.getNativeRequest(request, MultipartHttpServletRequest.class);
    boolean isMultipart = (multipartRequest != null || isMultipartContent(request));
    if (MultipartFile.class == parameter.getNestedParameterType()) {
        if (!isMultipart) {
            return null;
        }
        if (multipartRequest == null) {
            multipartRequest = new StandardMultipartHttpServletRequest(request);
        }
        return multipartRequest.getFile(name);
    } else if (isMultipartFileCollection(parameter)) {
        if (!isMultipart) {
            return null;
        }
        if (multipartRequest == null) {
            multipartRequest = new StandardMultipartHttpServletRequest(request);
        }
        List<MultipartFile> files = multipartRequest.getFiles(name);
        return (!files.isEmpty() ? files : null);
    } else if (isMultipartFileArray(parameter)) {
        if (!isMultipart) {
            return null;
        }
        if (multipartRequest == null) {
            multipartRequest = new StandardMultipartHttpServletRequest(request);
        }
        List<MultipartFile> files = multipartRequest.getFiles(name);
        return (!files.isEmpty() ? files.toArray(new MultipartFile[0]) : null);
    } else if (Part.class == parameter.getNestedParameterType()) {
        if (!isMultipart) {
            return null;
        }
        return request.getPart(name);
    } else if (isPartCollection(parameter)) {
        if (!isMultipart) {
            return null;
        }
        List<Part> parts = resolvePartList(request, name);
        return (!parts.isEmpty() ? parts : null);
    } else if (isPartArray(parameter)) {
        if (!isMultipart) {
            return null;
        }
        List<Part> parts = resolvePartList(request, name);
        return (!parts.isEmpty() ? parts.toArray(new Part[0]) : null);
    } else {
        return UNRESOLVABLE;
    }
}
Also used : MultipartFile(org.springframework.web.multipart.MultipartFile) Part(jakarta.servlet.http.Part) ArrayList(java.util.ArrayList) List(java.util.List) MultipartHttpServletRequest(org.springframework.web.multipart.MultipartHttpServletRequest) Nullable(org.springframework.lang.Nullable)

Example 74 with MultipartFile

use of org.springframework.web.multipart.MultipartFile in project spring-framework by spring-projects.

the class StandardMultipartHttpServletRequest method parseRequest.

private void parseRequest(HttpServletRequest request) {
    try {
        Collection<Part> parts = request.getParts();
        this.multipartParameterNames = new LinkedHashSet<>(parts.size());
        MultiValueMap<String, MultipartFile> files = new LinkedMultiValueMap<>(parts.size());
        for (Part part : parts) {
            String headerValue = part.getHeader(HttpHeaders.CONTENT_DISPOSITION);
            ContentDisposition disposition = ContentDisposition.parse(headerValue);
            String filename = disposition.getFilename();
            if (filename != null) {
                if (filename.startsWith("=?") && filename.endsWith("?=")) {
                    filename = MimeDelegate.decode(filename);
                }
                files.add(part.getName(), new StandardMultipartFile(part, filename));
            } else {
                this.multipartParameterNames.add(part.getName());
            }
        }
        setMultipartFiles(files);
    } catch (Throwable ex) {
        handleParseFailure(ex);
    }
}
Also used : MultipartFile(org.springframework.web.multipart.MultipartFile) ContentDisposition(org.springframework.http.ContentDisposition) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) Part(jakarta.servlet.http.Part)

Example 75 with MultipartFile

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);
    boolean condition1 = actualValue instanceof Optional;
    assertThat(condition1).isTrue();
    assertThat(((Optional<?>) actualValue).get()).as("Invalid result").isEqualTo(Collections.singletonList(expected));
    actualValue = resolver.resolveArgument(optionalMultipartFileList, null, webRequest, null);
    assertThat(actualValue instanceof Optional).isTrue();
    assertThat(((Optional<?>) actualValue).get()).as("Invalid result").isEqualTo(Collections.singletonList(expected));
}
Also used : MockMultipartFile(org.springframework.web.testfixture.servlet.MockMultipartFile) MockMultipartFile(org.springframework.web.testfixture.servlet.MockMultipartFile) MultipartFile(org.springframework.web.multipart.MultipartFile) MockMultipartHttpServletRequest(org.springframework.web.testfixture.servlet.MockMultipartHttpServletRequest) Optional(java.util.Optional) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) Test(org.junit.jupiter.api.Test)

Aggregations

MultipartFile (org.springframework.web.multipart.MultipartFile)272 File (java.io.File)151 IOException (java.io.IOException)71 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)71 MockMultipartFile (org.springframework.mock.web.MockMultipartFile)53 Test (org.junit.Test)41 FileInputStream (java.io.FileInputStream)37 ArrayList (java.util.ArrayList)30 JobConfig4DB (com.vip.saturn.job.console.mybatis.entity.JobConfig4DB)28 Test (org.junit.jupiter.api.Test)27 InputStream (java.io.InputStream)25 MultipartHttpServletRequest (org.springframework.web.multipart.MultipartHttpServletRequest)24 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)20 ByteArrayInputStream (java.io.ByteArrayInputStream)19 FileOutputStream (java.io.FileOutputStream)18 Date (java.util.Date)18 List (java.util.List)18 PostMapping (org.springframework.web.bind.annotation.PostMapping)17 MockMultipartFile (org.springframework.web.testfixture.servlet.MockMultipartFile)17 Path (java.nio.file.Path)15