commit
6800a0e43f
7
spring-framework-web/README.md
Normal file
7
spring-framework-web/README.md
Normal file
@ -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)
|
55
spring-framework-web/pom.xml
Normal file
55
spring-framework-web/pom.xml
Normal file
@ -0,0 +1,55 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>spring-framework-web</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>spring-framework-web</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-spring-5</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../parent-spring-5</relativePath>
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<commons-io.version>2.6</commons-io.version>
|
||||
<junit.version>4.12</junit.version>
|
||||
<org.assertj.assertj-core.version>3.15.0</org.assertj.assertj-core.version>
|
||||
<org.springframework.spring-mock.version>2.0.8</org.springframework.spring-mock.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>commons-io</groupId>
|
||||
<artifactId>commons-io</artifactId>
|
||||
<version>${commons-io.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${org.assertj.assertj-core.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-web</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-mock</artifactId>
|
||||
<version>${org.springframework.spring-mock.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>${junit.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
@ -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");
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user