diff --git a/spring-multipart-file/pom.xml b/spring-multipart-file/pom.xml new file mode 100644 index 0000000000..7b9eafeda5 --- /dev/null +++ b/spring-multipart-file/pom.xml @@ -0,0 +1,37 @@ + + + 4.0.0 + spring-multipart-file + 0.0.1-SNAPSHOT + spring-multipart-file + + + com.baeldung + parent-spring-5 + 0.0.1-SNAPSHOT + ../parent-spring-5 + + + + + org.springframework + spring-web + ${spring.version} + + + org.springframework + spring-mock + 2.0.8 + test + + + junit + junit + 4.12 + test + + + + diff --git a/spring-multipart-file/src/main/resources/targetFile.tmp b/spring-multipart-file/src/main/resources/targetFile.tmp new file mode 100644 index 0000000000..5e1c309dae --- /dev/null +++ b/spring-multipart-file/src/main/resources/targetFile.tmp @@ -0,0 +1 @@ +Hello World \ No newline at end of file diff --git a/spring-multipart-file/src/test/java/com/baeldung/multipart/file/ConvertMultipartFileExample.java b/spring-multipart-file/src/test/java/com/baeldung/multipart/file/ConvertMultipartFileExample.java new file mode 100644 index 0000000000..101e234921 --- /dev/null +++ b/spring-multipart-file/src/test/java/com/baeldung/multipart/file/ConvertMultipartFileExample.java @@ -0,0 +1,51 @@ +package com.baeldung.multipart.file; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +import org.junit.Test; +import org.springframework.mock.web.MockMultipartFile; +import org.springframework.web.multipart.MultipartFile; + +public class ConvertMultipartFileExample { + + @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"); + + OutputStream os = new FileOutputStream(file); + + os.write(multipartFile.getBytes()); + + os.close(); + } + + @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"); + OutputStream outStream = new FileOutputStream(targetFile); + outStream.write(buffer); + + outStream.close(); + } + + @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); + } +}