diff --git a/spring-web-modules/pom.xml b/spring-web-modules/pom.xml index 047dbaaad0..a9970e85c0 100644 --- a/spring-web-modules/pom.xml +++ b/spring-web-modules/pom.xml @@ -23,6 +23,7 @@ spring-mvc-basics-4 spring-mvc-basics-5 spring-mvc-crash + spring-mvc-file spring-mvc-forms-jsp spring-mvc-forms-thymeleaf spring-mvc-java diff --git a/spring-web-modules/spring-mvc-file/.gitignore b/spring-web-modules/spring-mvc-file/.gitignore new file mode 100644 index 0000000000..da9ec604ff --- /dev/null +++ b/spring-web-modules/spring-mvc-file/.gitignore @@ -0,0 +1,9 @@ +*.class + +#folders# +/target + +# Packaged files # +*.jar +*.war +*.ear \ No newline at end of file diff --git a/spring-web-modules/spring-mvc-file/README.md b/spring-web-modules/spring-mvc-file/README.md new file mode 100644 index 0000000000..c1622f80c2 --- /dev/null +++ b/spring-web-modules/spring-mvc-file/README.md @@ -0,0 +1,9 @@ +## Spring MVC File + + + +### The Course + + +### Relevant Articles: + diff --git a/spring-web-modules/spring-mvc-file/pom.xml b/spring-web-modules/spring-mvc-file/pom.xml new file mode 100644 index 0000000000..c6b063c785 --- /dev/null +++ b/spring-web-modules/spring-mvc-file/pom.xml @@ -0,0 +1,50 @@ + + + 4.0.0 + spring-mvc-file + 0.1-SNAPSHOT + spring-mvc-file + jar + + + com.baeldung + parent-boot-2 + 0.0.1-SNAPSHOT + ../../parent-boot-2 + + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-test + test + + + commons-io + commons-io + ${commons-io.version} + + + + + + spring-mvc-file + + + org.springframework.boot + spring-boot-maven-plugin + + com.baeldung.Application + JAR + + + + + + \ No newline at end of file diff --git a/spring-web-modules/spring-mvc-file/src/main/java/com/baeldung/Application.java b/spring-web-modules/spring-mvc-file/src/main/java/com/baeldung/Application.java new file mode 100644 index 0000000000..d58049fb35 --- /dev/null +++ b/spring-web-modules/spring-mvc-file/src/main/java/com/baeldung/Application.java @@ -0,0 +1,11 @@ +package com.baeldung; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class Application { + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } +} diff --git a/spring-web-modules/spring-mvc-file/src/main/java/com/baeldung/file/CustomMultipartFile.java b/spring-web-modules/spring-mvc-file/src/main/java/com/baeldung/file/CustomMultipartFile.java new file mode 100644 index 0000000000..c68729588f --- /dev/null +++ b/spring-web-modules/spring-mvc-file/src/main/java/com/baeldung/file/CustomMultipartFile.java @@ -0,0 +1,60 @@ +package com.baeldung.file; + +import java.io.ByteArrayInputStream; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; + +import org.springframework.web.multipart.MultipartFile; + +public class CustomMultipartFile implements MultipartFile { + private byte[] input; + + public CustomMultipartFile(byte[] input) { + this.input = input; + } + + @Override + public String getName() { + return null; + } + + @Override + public String getOriginalFilename() { + return null; + } + + @Override + public String getContentType() { + return null; + } + + @Override + public boolean isEmpty() { + return input == null || input.length == 0; + } + + @Override + public long getSize() { + return input.length; + } + + @Override + public byte[] getBytes() throws IOException { + return input; + } + + @Override + public InputStream getInputStream() throws IOException { + return new ByteArrayInputStream(input); + } + + @Override + public void transferTo(File destination) throws IOException, IllegalStateException { + try(FileOutputStream fos = new FileOutputStream(destination)) { + fos.write(input); + } + } + +} diff --git a/spring-web-modules/spring-mvc-file/src/test/java/com/baeldung/file/CustomMultipartFileUnitTest.java b/spring-web-modules/spring-mvc-file/src/test/java/com/baeldung/file/CustomMultipartFileUnitTest.java new file mode 100644 index 0000000000..1aa07766ca --- /dev/null +++ b/spring-web-modules/spring-mvc-file/src/test/java/com/baeldung/file/CustomMultipartFileUnitTest.java @@ -0,0 +1,49 @@ +package com.baeldung.file; + +import java.io.IOException; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.springframework.mock.web.MockMultipartFile; + +class CustomMultipartFileUnitTest { + + @Test + void whenProvidingByteArray_thenMultipartFileCreated() throws IOException { + byte[] inputArray = "Test String".getBytes(); + CustomMultipartFile customMultipartFile = new CustomMultipartFile(inputArray); + Assertions.assertFalse(customMultipartFile.isEmpty()); + Assertions.assertArrayEquals(inputArray, customMultipartFile.getBytes()); + Assertions.assertEquals(inputArray.length, customMultipartFile.getSize()); + } + + @Test + void whenProvidingEmptyByteArray_thenMockMultipartFileIsEmpty() throws IOException { + byte[] inputArray = "".getBytes(); + MockMultipartFile mockMultipartFile = new MockMultipartFile("tempFileName", inputArray); + Assertions.assertTrue(mockMultipartFile.isEmpty()); + } + + @Test + void whenProvidingNullByteArray_thenMockMultipartFileIsEmpty() throws IOException { + byte[] inputArray = null; + MockMultipartFile mockMultipartFile = new MockMultipartFile("tempFileName", inputArray); + Assertions.assertTrue(mockMultipartFile.isEmpty()); + } + + @Test + void whenProvidingByteArray_thenMultipartFileInputSizeMatches() throws IOException { + byte[] inputArray = "Testing String".getBytes(); + CustomMultipartFile customMultipartFile = new CustomMultipartFile(inputArray); + Assertions.assertEquals(inputArray.length, customMultipartFile.getSize()); + } + + @Test + void whenProvidingByteArray_thenMockMultipartFileCreated() throws IOException { + byte[] inputArray = "Test String".getBytes(); + MockMultipartFile mockMultipartFile = new MockMultipartFile("tempFileName", inputArray); + Assertions.assertFalse(mockMultipartFile.isEmpty()); + Assertions.assertArrayEquals(inputArray, mockMultipartFile.getBytes()); + Assertions.assertEquals(inputArray.length, mockMultipartFile.getSize()); + } +}