|
| 1 | +package com.baeldung.web.upload.client; |
| 2 | + |
| 3 | +import org.springframework.core.io.FileSystemResource; |
| 4 | +import org.springframework.core.io.Resource; |
| 5 | +import org.springframework.http.HttpEntity; |
| 6 | +import org.springframework.http.HttpHeaders; |
| 7 | +import org.springframework.http.MediaType; |
| 8 | +import org.springframework.http.ResponseEntity; |
| 9 | +import org.springframework.util.LinkedMultiValueMap; |
| 10 | +import org.springframework.util.MultiValueMap; |
| 11 | +import org.springframework.web.client.RestTemplate; |
| 12 | + |
| 13 | +import java.io.IOException; |
| 14 | +import java.nio.file.Files; |
| 15 | +import java.nio.file.Path; |
| 16 | + |
| 17 | +public class MultipartFileUploadClient { |
| 18 | + |
| 19 | + public static void main(String[] args) throws IOException { |
| 20 | + uploadSingleFile(); |
| 21 | + uploadMultipleFile(); |
| 22 | + } |
| 23 | + |
| 24 | + private static void uploadSingleFile() throws IOException { |
| 25 | + HttpHeaders headers = new HttpHeaders(); |
| 26 | + headers.setContentType(MediaType.MULTIPART_FORM_DATA); |
| 27 | + |
| 28 | + MultiValueMap<String, Object> body = new LinkedMultiValueMap<>(); |
| 29 | + body.add("file", getTestFile()); |
| 30 | + |
| 31 | + |
| 32 | + HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers); |
| 33 | + String serverUrl = "http://localhost:8082/spring-rest/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(); |
| 41 | + headers.setContentType(MediaType.MULTIPART_FORM_DATA); |
| 42 | + |
| 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:8082/spring-rest/fileserver/multiplefileupload/"; |
| 50 | + RestTemplate restTemplate = new RestTemplate(); |
| 51 | + ResponseEntity<String> response = restTemplate.postForEntity(serverUrl, requestEntity, String.class); |
| 52 | + System.out.println("Response code: " + response.getStatusCode()); |
| 53 | + } |
| 54 | + |
| 55 | + public static Resource getTestFile() throws IOException { |
| 56 | + Path testFile = Files.createTempFile("test-file", ".txt"); |
| 57 | + System.out.println("Creating and Uploading Test File: " + testFile); |
| 58 | + Files.write(testFile, "Hello World !!, This is a test file.".getBytes()); |
| 59 | + return new FileSystemResource(testFile.toFile()); |
| 60 | + } |
| 61 | +} |
0 commit comments