diff --git a/spring-web-modules/spring-mvc-java-2/pom.xml b/spring-web-modules/spring-mvc-java-2/pom.xml
index d88a9c320a..b1b5a797f8 100644
--- a/spring-web-modules/spring-mvc-java-2/pom.xml
+++ b/spring-web-modules/spring-mvc-java-2/pom.xml
@@ -41,6 +41,11 @@
jaxb-runtime
${jaxb-runtime.version}
+
+ commons-fileupload
+ commons-fileupload
+ ${commons-fileupload.version}
+
@@ -57,6 +62,7 @@
4.0.1
5.2.2.RELEASE
2.3.5
+ 1.5
\ No newline at end of file
diff --git a/spring-web-modules/spring-mvc-java-2/src/test/java/com/baeldung/multipart/file/ConvertFileToMultipartFileUnitTest.java b/spring-web-modules/spring-mvc-java-2/src/test/java/com/baeldung/multipart/file/ConvertFileToMultipartFileUnitTest.java
new file mode 100644
index 0000000000..7d99f7f9bb
--- /dev/null
+++ b/spring-web-modules/spring-mvc-java-2/src/test/java/com/baeldung/multipart/file/ConvertFileToMultipartFileUnitTest.java
@@ -0,0 +1,45 @@
+package com.baeldung.multipart.file;
+
+import static org.junit.Assert.assertEquals;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.nio.file.Files;
+
+import org.apache.commons.fileupload.FileItem;
+import org.apache.commons.fileupload.disk.DiskFileItem;
+import org.apache.commons.io.IOUtils;
+import org.junit.jupiter.api.Test;
+import org.springframework.web.multipart.MultipartFile;
+import org.springframework.web.multipart.commons.CommonsMultipartFile;
+import org.springframework.mock.web.MockMultipartFile;
+
+public class ConvertFileToMultipartFileUnitTest {
+
+ @Test
+ public void givenFile_whenCreateMultipartFile_thenContentMatch() throws IOException {
+ File file = new File("src/main/resources/targetFile.tmp");
+ byte[] fileBytes = Files.readAllBytes(file.toPath());
+ MultipartFile multipartFile = new MockMultipartFile("file", file.getName(), "text/plain", fileBytes);
+ String fileContent = new String(multipartFile.getBytes());
+ assertEquals("Hello World", fileContent);
+ assertEquals("targetFile.tmp", multipartFile.getOriginalFilename());
+ }
+
+ @Test
+ public void givenFile_whenCreateMultipartFileUsingCommonsMultipart_thenContentMatch() throws IOException {
+ File file = new File("src/main/resources/targetFile.tmp");
+ FileItem fileItem = new DiskFileItem("file", Files.probeContentType(file.toPath()), false, file.getName(), (int) file.length(), file.getParentFile());
+ InputStream input = new FileInputStream(file);
+ OutputStream outputStream = fileItem.getOutputStream();
+ IOUtils.copy(input, outputStream);
+ MultipartFile multipartFile = new CommonsMultipartFile(fileItem);
+ String fileContent = new String(multipartFile.getBytes());
+ assertEquals("Hello World", fileContent);
+ assertEquals("targetFile.tmp", multipartFile.getOriginalFilename());
+ }
+
+}