Skip to content

Commit c719cf2

Browse files
committed
Updated Client and Server for Multiple File Upload
1 parent 03c5e4c commit c719cf2

2 files changed

Lines changed: 45 additions & 9 deletions

File tree

‎spring-rest/src/main/java/com/baeldung/web/upload/client/MultipartFileUploadClient.java‎

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.baeldung.web.upload.client;
22

3-
43
import org.springframework.core.io.FileSystemResource;
54
import org.springframework.core.io.Resource;
65
import org.springframework.http.HttpEntity;
@@ -18,14 +17,36 @@
1817
public class MultipartFileUploadClient {
1918

2019
public static void main(String[] args) throws IOException {
21-
MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
20+
uploadSingleFile();
21+
uploadMultipleFile();
22+
}
23+
24+
private static void uploadSingleFile() throws IOException {
2225
HttpHeaders headers = new HttpHeaders();
23-
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers);
26+
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
2427

28+
MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
2529
body.add("file", getTestFile());
30+
31+
32+
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers);
33+
String serverUrl = "http://localhost:8080/fileserver/singlefileupload/";
34+
RestTemplate restTemplate = new RestTemplate();
35+
ResponseEntity<String> response = restTemplate.postForEntity(serverUrl, requestEntity, String.class);
36+
System.out.println("Response code: " + response.getStatusCode());
37+
}
38+
39+
private static void uploadMultipleFile() throws IOException {
40+
HttpHeaders headers = new HttpHeaders();
2641
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
2742

28-
String serverUrl = "http://localhost:8080/fileserver/";
43+
MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
44+
body.add("files", getTestFile());
45+
body.add("files", getTestFile());
46+
body.add("files", getTestFile());
47+
48+
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers);
49+
String serverUrl = "http://localhost:8080/fileserver/multiplefileupload/";
2950
RestTemplate restTemplate = new RestTemplate();
3051
ResponseEntity<String> response = restTemplate.postForEntity(serverUrl, requestEntity, String.class);
3152
System.out.println("Response code: " + response.getStatusCode());

‎spring-rest/src/main/java/com/baeldung/web/upload/controller/FileServerResource.java‎

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,35 @@
99
import org.springframework.web.multipart.MultipartFile;
1010

1111
import java.io.IOException;
12+
import java.util.List;
1213

1314
@RestController
1415
@RequestMapping("/fileserver")
1516
public class FileServerResource {
1617

17-
@RequestMapping(method = RequestMethod.POST)
18-
public ResponseEntity<String> processFile(@RequestParam("file") MultipartFile multipartFile) throws IOException {
18+
@RequestMapping(path = "/singlefileupload/", method = RequestMethod.POST)
19+
public ResponseEntity<String> processFile(@RequestParam("file") MultipartFile file) throws IOException {
1920

20-
byte[] bytes = multipartFile.getBytes();
21+
byte[] bytes = file.getBytes();
2122

22-
System.out.println("File Name: " + multipartFile.getOriginalFilename());
23-
System.out.println("File Content Type: " + multipartFile.getContentType());
23+
System.out.println("File Name: " + file.getOriginalFilename());
24+
System.out.println("File Content Type: " + file.getContentType());
2425
System.out.println("File Content:\n" + new String(bytes));
2526

2627
return (new ResponseEntity<>("Successful", null, HttpStatus.OK));
2728
}
29+
30+
@RequestMapping(path = "/multiplefileupload/", method = RequestMethod.POST)
31+
public ResponseEntity<String> processFile(@RequestParam("files") List<MultipartFile> files) throws IOException {
32+
33+
for (MultipartFile file : files) {
34+
byte[] bytes = file.getBytes();
35+
36+
System.out.println("File Name: " + file.getOriginalFilename());
37+
System.out.println("File Content Type: " + file.getContentType());
38+
System.out.println("File Content:\n" + new String(bytes));
39+
}
40+
41+
return (new ResponseEntity<>("Successful", null, HttpStatus.OK));
42+
}
2843
}

0 commit comments

Comments
 (0)