Added Example Application For FileUpload using Spring RestTemplated

This commit is contained in:
micropatel 2018-07-04 22:00:38 -03:00
parent b8c78d9a8e
commit 03c5e4c03a
3 changed files with 85 additions and 0 deletions

View File

@ -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);
}
}

View File

@ -0,0 +1,40 @@
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 {
MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
HttpHeaders headers = new HttpHeaders();
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers);
body.add("file", getTestFile());
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
String serverUrl = "http://localhost:8080/fileserver/";
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());
}
}

View File

@ -0,0 +1,28 @@
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;
@RestController
@RequestMapping("/fileserver")
public class FileServerResource {
@RequestMapping(method = RequestMethod.POST)
public ResponseEntity<String> processFile(@RequestParam("file") MultipartFile multipartFile) throws IOException {
byte[] bytes = multipartFile.getBytes();
System.out.println("File Name: " + multipartFile.getOriginalFilename());
System.out.println("File Content Type: " + multipartFile.getContentType());
System.out.println("File Content:\n" + new String(bytes));
return (new ResponseEntity<>("Successful", null, HttpStatus.OK));
}
}