Updated Client and Server for Multiple File Upload
This commit is contained in:
parent
03c5e4c03a
commit
c719cf25f2
|
@ -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<String, Object> body = new LinkedMultiValueMap<>();
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
HttpEntity<MultiValueMap<String, Object>> 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<String, Object> body = new LinkedMultiValueMap<>();
|
||||
body.add("file", getTestFile());
|
||||
|
||||
|
||||
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers);
|
||||
String serverUrl = "http://localhost:8080/fileserver/singlefileupload/";
|
||||
RestTemplate restTemplate = new RestTemplate();
|
||||
ResponseEntity<String> 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<String, Object> body = new LinkedMultiValueMap<>();
|
||||
body.add("files", getTestFile());
|
||||
body.add("files", getTestFile());
|
||||
body.add("files", getTestFile());
|
||||
|
||||
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers);
|
||||
String serverUrl = "http://localhost:8080/fileserver/multiplefileupload/";
|
||||
RestTemplate restTemplate = new RestTemplate();
|
||||
ResponseEntity<String> response = restTemplate.postForEntity(serverUrl, requestEntity, String.class);
|
||||
System.out.println("Response code: " + response.getStatusCode());
|
||||
|
|
|
@ -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<String> processFile(@RequestParam("file") MultipartFile multipartFile) throws IOException {
|
||||
@RequestMapping(path = "/singlefileupload/", method = RequestMethod.POST)
|
||||
public ResponseEntity<String> 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<String> processFile(@RequestParam("files") List<MultipartFile> 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));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue