Updated Client and Server for Multiple File Upload

This commit is contained in:
micropatel 2018-07-10 01:19:03 -03:00
parent 03c5e4c03a
commit c719cf25f2
2 changed files with 47 additions and 11 deletions

View File

@ -1,6 +1,5 @@
package com.baeldung.web.upload.client; package com.baeldung.web.upload.client;
import org.springframework.core.io.FileSystemResource; import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;
import org.springframework.http.HttpEntity; import org.springframework.http.HttpEntity;
@ -18,14 +17,36 @@ import java.nio.file.Path;
public class MultipartFileUploadClient { public class MultipartFileUploadClient {
public static void main(String[] args) throws IOException { public static void main(String[] args) throws IOException {
MultiValueMap<String, Object> body = new LinkedMultiValueMap<>(); uploadSingleFile();
HttpHeaders headers = new HttpHeaders(); uploadMultipleFile();
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers); }
body.add("file", getTestFile()); private static void uploadSingleFile() throws IOException {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA); 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(); RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> response = restTemplate.postForEntity(serverUrl, requestEntity, String.class); ResponseEntity<String> response = restTemplate.postForEntity(serverUrl, requestEntity, String.class);
System.out.println("Response code: " + response.getStatusCode()); System.out.println("Response code: " + response.getStatusCode());

View File

@ -9,20 +9,35 @@ import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import java.io.IOException; import java.io.IOException;
import java.util.List;
@RestController @RestController
@RequestMapping("/fileserver") @RequestMapping("/fileserver")
public class FileServerResource { public class FileServerResource {
@RequestMapping(method = RequestMethod.POST) @RequestMapping(path = "/singlefileupload/", method = RequestMethod.POST)
public ResponseEntity<String> processFile(@RequestParam("file") MultipartFile multipartFile) throws IOException { 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 Name: " + file.getOriginalFilename());
System.out.println("File Content Type: " + multipartFile.getContentType()); System.out.println("File Content Type: " + file.getContentType());
System.out.println("File Content:\n" + new String(bytes)); System.out.println("File Content:\n" + new String(bytes));
return (new ResponseEntity<>("Successful", null, HttpStatus.OK)); 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));
}
} }