Merge pull request #4628 from micropatel/master
patelmayurm@gmail.com. Added Example for FileUpload using Spring RestTemplate
This commit is contained in:
commit
e3bd6d493a
|
@ -0,0 +1,17 @@
|
|||
package com.baeldung.web.upload.app;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
|
||||
@EnableAutoConfiguration
|
||||
@ComponentScan("com.baeldung.web.upload")
|
||||
@SpringBootApplication
|
||||
public class Application extends SpringBootServletInitializer {
|
||||
|
||||
public static void main(final String[] args) {
|
||||
SpringApplication.run(Application.class, args);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,61 @@
|
|||
package com.baeldung.web.upload.client;
|
||||
|
||||
import org.springframework.core.io.FileSystemResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.util.LinkedMultiValueMap;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
|
||||
public class MultipartFileUploadClient {
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
uploadSingleFile();
|
||||
uploadMultipleFile();
|
||||
}
|
||||
|
||||
private static void uploadSingleFile() throws IOException {
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
|
||||
|
||||
MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
|
||||
body.add("file", getTestFile());
|
||||
|
||||
|
||||
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers);
|
||||
String serverUrl = "http://localhost:8082/spring-rest/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:8082/spring-rest/fileserver/multiplefileupload/";
|
||||
RestTemplate restTemplate = new RestTemplate();
|
||||
ResponseEntity<String> response = restTemplate.postForEntity(serverUrl, requestEntity, String.class);
|
||||
System.out.println("Response code: " + response.getStatusCode());
|
||||
}
|
||||
|
||||
public static Resource getTestFile() throws IOException {
|
||||
Path testFile = Files.createTempFile("test-file", ".txt");
|
||||
System.out.println("Creating and Uploading Test File: " + testFile);
|
||||
Files.write(testFile, "Hello World !!, This is a test file.".getBytes());
|
||||
return new FileSystemResource(testFile.toFile());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,43 @@
|
|||
package com.baeldung.web.upload.controller;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
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(path = "/singlefileupload/", method = RequestMethod.POST)
|
||||
public ResponseEntity<String> processFile(@RequestParam("file") MultipartFile file) throws IOException {
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
@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