Revert "BAEL-4134"
This commit is contained in:
committed by
GitHub
parent
dffa1f64e6
commit
485b4e3e99
@@ -3,4 +3,5 @@
|
||||
- [Cache Headers in Spring MVC](https://www.baeldung.com/spring-mvc-cache-headers)
|
||||
- [Working with Date Parameters in Spring](https://www.baeldung.com/spring-date-parameters)
|
||||
- [Spring MVC @PathVariable with a dot (.) gets truncated](https://www.baeldung.com/spring-mvc-pathvariable-dot)
|
||||
- [A Quick Guide to Spring MVC Matrix Variables](https://www.baeldung.com/spring-mvc-matrix-variables)
|
||||
- [A Quick Guide to Spring MVC Matrix Variables](https://www.baeldung.com/spring-mvc-matrix-variables)
|
||||
- [Converting a Spring MultipartFile to a File](https://www.baeldung.com/converting-spring-multipartfile-to-a-file)
|
||||
|
||||
@@ -31,6 +31,11 @@
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
<version>${jackson.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-io</groupId>
|
||||
<artifactId>commons-io</artifactId>
|
||||
<version>${commons-io.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
@@ -49,4 +54,4 @@
|
||||
</properties>
|
||||
|
||||
|
||||
</project>
|
||||
</project>
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.multiparttesting;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
@RestController
|
||||
public class MultipartPostRequestController {
|
||||
|
||||
@PostMapping(path = "/upload")
|
||||
public ResponseEntity<String> uploadFile(@RequestParam("file") MultipartFile file) {
|
||||
return file.isEmpty() ? new ResponseEntity<String>(HttpStatus.NOT_FOUND) : new ResponseEntity<String>(HttpStatus.OK);
|
||||
}
|
||||
}
|
||||
1
spring-mvc-java-2/src/main/resources/targetFile.tmp
Normal file
1
spring-mvc-java-2/src/main/resources/targetFile.tmp
Normal file
@@ -0,0 +1 @@
|
||||
Hello World
|
||||
@@ -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");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.baeldung.multiparttesting;
|
||||
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.multipart;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.mock.web.MockMultipartFile;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.web.WebAppConfiguration;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
|
||||
import com.baeldung.matrix.config.MatrixWebConfig;
|
||||
|
||||
@WebAppConfiguration
|
||||
@ContextConfiguration(classes = { MatrixWebConfig.class, MultipartPostRequestController.class })
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
public class MultipartPostRequestControllerUnitTest {
|
||||
|
||||
@Autowired
|
||||
private WebApplicationContext webApplicationContext;
|
||||
|
||||
@Test
|
||||
public void whenFileUploaded_thenVerifyStatus() throws Exception {
|
||||
MockMultipartFile file = new MockMultipartFile("file", "hello.txt", MediaType.TEXT_PLAIN_VALUE, "Hello, World!".getBytes());
|
||||
|
||||
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
|
||||
mockMvc.perform(multipart("/upload").file(file)).andExpect(status().isOk());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user