diff --git a/spring-rest/src/main/java/com/baeldung/web/upload/app/Application.java b/spring-rest/src/main/java/com/baeldung/web/upload/app/Application.java new file mode 100644 index 0000000000..d6821196eb --- /dev/null +++ b/spring-rest/src/main/java/com/baeldung/web/upload/app/Application.java @@ -0,0 +1,17 @@ +package com.baeldung.web.upload.app; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; +import org.springframework.context.annotation.ComponentScan; + +@EnableAutoConfiguration +@ComponentScan("com.baeldung.web.upload") +@SpringBootApplication +public class Application extends SpringBootServletInitializer { + + public static void main(final String[] args) { + SpringApplication.run(Application.class, args); + } +} \ No newline at end of file diff --git a/spring-rest/src/main/java/com/baeldung/web/upload/client/MultipartFileUploadClient.java b/spring-rest/src/main/java/com/baeldung/web/upload/client/MultipartFileUploadClient.java new file mode 100644 index 0000000000..547aec17a0 --- /dev/null +++ b/spring-rest/src/main/java/com/baeldung/web/upload/client/MultipartFileUploadClient.java @@ -0,0 +1,61 @@ +package com.baeldung.web.upload.client; + +import org.springframework.core.io.FileSystemResource; +import org.springframework.core.io.Resource; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.util.LinkedMultiValueMap; +import org.springframework.util.MultiValueMap; +import org.springframework.web.client.RestTemplate; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; + +public class MultipartFileUploadClient { + + public static void main(String[] args) throws IOException { + uploadSingleFile(); + uploadMultipleFile(); + } + + private static void uploadSingleFile() throws IOException { + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.MULTIPART_FORM_DATA); + + MultiValueMap body = new LinkedMultiValueMap<>(); + body.add("file", getTestFile()); + + + HttpEntity> requestEntity = new HttpEntity<>(body, headers); + String serverUrl = "http://localhost:8082/spring-rest/fileserver/singlefileupload/"; + RestTemplate restTemplate = new RestTemplate(); + ResponseEntity response = restTemplate.postForEntity(serverUrl, requestEntity, String.class); + System.out.println("Response code: " + response.getStatusCode()); + } + + private static void uploadMultipleFile() throws IOException { + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.MULTIPART_FORM_DATA); + + MultiValueMap body = new LinkedMultiValueMap<>(); + body.add("files", getTestFile()); + body.add("files", getTestFile()); + body.add("files", getTestFile()); + + HttpEntity> requestEntity = new HttpEntity<>(body, headers); + String serverUrl = "http://localhost:8082/spring-rest/fileserver/multiplefileupload/"; + RestTemplate restTemplate = new RestTemplate(); + ResponseEntity response = restTemplate.postForEntity(serverUrl, requestEntity, String.class); + System.out.println("Response code: " + response.getStatusCode()); + } + + public static Resource getTestFile() throws IOException { + Path testFile = Files.createTempFile("test-file", ".txt"); + System.out.println("Creating and Uploading Test File: " + testFile); + Files.write(testFile, "Hello World !!, This is a test file.".getBytes()); + return new FileSystemResource(testFile.toFile()); + } +} diff --git a/spring-rest/src/main/java/com/baeldung/web/upload/controller/FileServerResource.java b/spring-rest/src/main/java/com/baeldung/web/upload/controller/FileServerResource.java new file mode 100644 index 0000000000..3863a8f20d --- /dev/null +++ b/spring-rest/src/main/java/com/baeldung/web/upload/controller/FileServerResource.java @@ -0,0 +1,43 @@ +package com.baeldung.web.upload.controller; + +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.multipart.MultipartFile; + +import java.io.IOException; +import java.util.List; + +@RestController +@RequestMapping("/fileserver") +public class FileServerResource { + + @RequestMapping(path = "/singlefileupload/", method = RequestMethod.POST) + public ResponseEntity processFile(@RequestParam("file") MultipartFile file) throws IOException { + + byte[] bytes = file.getBytes(); + + System.out.println("File Name: " + file.getOriginalFilename()); + System.out.println("File Content Type: " + file.getContentType()); + System.out.println("File Content:\n" + new String(bytes)); + + return (new ResponseEntity<>("Successful", null, HttpStatus.OK)); + } + + @RequestMapping(path = "/multiplefileupload/", method = RequestMethod.POST) + public ResponseEntity processFile(@RequestParam("files") List files) throws IOException { + + for (MultipartFile file : files) { + byte[] bytes = file.getBytes(); + + System.out.println("File Name: " + file.getOriginalFilename()); + System.out.println("File Content Type: " + file.getContentType()); + System.out.println("File Content:\n" + new String(bytes)); + } + + return (new ResponseEntity<>("Successful", null, HttpStatus.OK)); + } +} \ No newline at end of file