|
1 | 1 | package com.baeldung.web.upload.client; |
2 | 2 |
|
3 | | - |
4 | 3 | import org.springframework.core.io.FileSystemResource; |
5 | 4 | import org.springframework.core.io.Resource; |
6 | 5 | import org.springframework.http.HttpEntity; |
|
18 | 17 | public class MultipartFileUploadClient { |
19 | 18 |
|
20 | 19 | 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 { |
22 | 25 | HttpHeaders headers = new HttpHeaders(); |
23 | | - HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers); |
| 26 | + headers.setContentType(MediaType.MULTIPART_FORM_DATA); |
24 | 27 |
|
| 28 | + MultiValueMap<String, Object> body = new LinkedMultiValueMap<>(); |
25 | 29 | 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(); |
26 | 41 | headers.setContentType(MediaType.MULTIPART_FORM_DATA); |
27 | 42 |
|
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/"; |
29 | 50 | RestTemplate restTemplate = new RestTemplate(); |
30 | 51 | ResponseEntity<String> response = restTemplate.postForEntity(serverUrl, requestEntity, String.class); |
31 | 52 | System.out.println("Response code: " + response.getStatusCode()); |
|
0 commit comments