diff --git a/spring-framework-web/README.md b/spring-framework-web/README.md new file mode 100644 index 0000000000..9c21e93b83 --- /dev/null +++ b/spring-framework-web/README.md @@ -0,0 +1,7 @@ +## Spring Framework Web + +This module contains articles about the Spring Framework for the Web. + +### Relevant Articles + +- [Converting a Spring MultipartFile to a File](https://www.baeldung.com/converting-spring-multipartfile-to-a-file) diff --git a/spring-framework-web/pom.xml b/spring-framework-web/pom.xml new file mode 100644 index 0000000000..50029b4ba8 --- /dev/null +++ b/spring-framework-web/pom.xml @@ -0,0 +1,55 @@ + + + 4.0.0 + spring-framework-web + 0.0.1-SNAPSHOT + spring-framework-web + + + com.baeldung + parent-spring-5 + 0.0.1-SNAPSHOT + ../parent-spring-5 + + + + 2.6 + 4.12 + 3.15.0 + 2.0.8 + + + + + commons-io + commons-io + ${commons-io.version} + + + org.assertj + assertj-core + ${org.assertj.assertj-core.version} + test + + + org.springframework + spring-web + ${spring.version} + + + org.springframework + spring-mock + ${org.springframework.spring-mock.version} + test + + + junit + junit + ${junit.version} + test + + + + diff --git a/spring-framework-web/src/test/java/com/baeldung/multipart/file/ConvertMultipartFileUnitTest.java b/spring-framework-web/src/test/java/com/baeldung/multipart/file/ConvertMultipartFileUnitTest.java new file mode 100644 index 0000000000..786f2355c1 --- /dev/null +++ b/spring-framework-web/src/test/java/com/baeldung/multipart/file/ConvertMultipartFileUnitTest.java @@ -0,0 +1,73 @@ +package com.baeldung.multipart.file; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +import org.apache.commons.io.FileUtils; +import org.junit.Test; +import org.springframework.mock.web.MockMultipartFile; +import org.springframework.web.multipart.MultipartFile; + +public class ConvertMultipartFileUnitTest { + + /** + * Example of converting a {@link MultipartFile} to a {@link File} using {@link MultipartFile#getBytes()}. + * + * @throws IOException + */ + @Test + public void whenGetBytes_thenOK() throws IOException { + MultipartFile multipartFile = new MockMultipartFile("sourceFile.tmp", "Hello World".getBytes()); + + File file = new File("src/main/resources/targetFile.tmp"); + + try (OutputStream os = new FileOutputStream(file)) { + os.write(multipartFile.getBytes()); + } + + assertThat(FileUtils.readFileToString(new File("src/main/resources/targetFile.tmp"), "UTF-8")).isEqualTo("Hello World"); + } + + /** + * Example of converting a {@link MultipartFile} to a {@link File} using {@link MultipartFile#getInputStream()}. + * + * @throws IOException + */ + @Test + public void whenGetInputStream_thenOK() throws IOException { + MultipartFile multipartFile = new MockMultipartFile("sourceFile.tmp", "Hello World".getBytes()); + + InputStream initialStream = multipartFile.getInputStream(); + byte[] buffer = new byte[initialStream.available()]; + initialStream.read(buffer); + + File targetFile = new File("src/main/resources/targetFile.tmp"); + + try (OutputStream outStream = new FileOutputStream(targetFile)) { + outStream.write(buffer); + } + + assertThat(FileUtils.readFileToString(new File("src/main/resources/targetFile.tmp"), "UTF-8")).isEqualTo("Hello World"); + } + + /** + * Example of converting a {@link MultipartFile} to a {@link File} using {@link MultipartFile#transferTo(File)}. + * + * @throws IOException + */ + @Test + public void whenTransferTo_thenOK() throws IllegalStateException, IOException { + MultipartFile multipartFile = new MockMultipartFile("sourceFile.tmp", "Hello World".getBytes()); + + File file = new File("src/main/resources/targetFile.tmp"); + + multipartFile.transferTo(file); + + assertThat(FileUtils.readFileToString(new File("src/main/resources/targetFile.tmp"), "UTF-8")).isEqualTo("Hello World"); + } +}