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 index 2045e80334..0e9f95ad33 100644 --- 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 @@ -1,6 +1,5 @@ package com.baeldung.web.upload.client; - import org.springframework.core.io.FileSystemResource; import org.springframework.core.io.Resource; import org.springframework.http.HttpEntity; @@ -18,14 +17,36 @@ import java.nio.file.Path; public class MultipartFileUploadClient { public static void main(String[] args) throws IOException { - MultiValueMap body = new LinkedMultiValueMap<>(); - HttpHeaders headers = new HttpHeaders(); - HttpEntity> requestEntity = new HttpEntity<>(body, headers); + uploadSingleFile(); + uploadMultipleFile(); + } - body.add("file", getTestFile()); + private static void uploadSingleFile() throws IOException { + HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.MULTIPART_FORM_DATA); - String serverUrl = "http://localhost:8080/fileserver/"; + MultiValueMap body = new LinkedMultiValueMap<>(); + body.add("file", getTestFile()); + + + HttpEntity> requestEntity = new HttpEntity<>(body, headers); + String serverUrl = "http://localhost:8080/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:8080/fileserver/multiplefileupload/"; RestTemplate restTemplate = new RestTemplate(); ResponseEntity response = restTemplate.postForEntity(serverUrl, requestEntity, String.class); System.out.println("Response code: " + response.getStatusCode()); 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 index 5198c80cff..3863a8f20d 100644 --- 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 @@ -9,20 +9,35 @@ 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(method = RequestMethod.POST) - public ResponseEntity processFile(@RequestParam("file") MultipartFile multipartFile) throws IOException { + @RequestMapping(path = "/singlefileupload/", method = RequestMethod.POST) + public ResponseEntity processFile(@RequestParam("file") MultipartFile file) throws IOException { - byte[] bytes = multipartFile.getBytes(); + byte[] bytes = file.getBytes(); - System.out.println("File Name: " + multipartFile.getOriginalFilename()); - System.out.println("File Content Type: " + multipartFile.getContentType()); + 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