Skip to content

Commit e3bd6d4

Browse files
authored
Merge pull request eugenp#4628 from micropatel/master
patelmayurm@gmail.com. Added Example for FileUpload using Spring RestTemplate
2 parents 4fface3 + 5d7b57c commit e3bd6d4

3 files changed

Lines changed: 121 additions & 0 deletions

File tree

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.baeldung.web.upload.app;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
5+
import org.springframework.boot.autoconfigure.SpringBootApplication;
6+
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
7+
import org.springframework.context.annotation.ComponentScan;
8+
9+
@EnableAutoConfiguration
10+
@ComponentScan("com.baeldung.web.upload")
11+
@SpringBootApplication
12+
public class Application extends SpringBootServletInitializer {
13+
14+
public static void main(final String[] args) {
15+
SpringApplication.run(Application.class, args);
16+
}
17+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.baeldung.web.upload.controller;
2+
3+
import org.springframework.http.HttpStatus;
4+
import org.springframework.http.ResponseEntity;
5+
import org.springframework.web.bind.annotation.RequestMapping;
6+
import org.springframework.web.bind.annotation.RequestMethod;
7+
import org.springframework.web.bind.annotation.RequestParam;
8+
import org.springframework.web.bind.annotation.RestController;
9+
import org.springframework.web.multipart.MultipartFile;
10+
11+
import java.io.IOException;
12+
import java.util.List;
13+
14+
@RestController
15+
@RequestMapping("/fileserver")
16+
public class FileServerResource {
17+
18+
@RequestMapping(path = "/singlefileupload/", method = RequestMethod.POST)
19+
public ResponseEntity<String> processFile(@RequestParam("file") MultipartFile file) throws IOException {
20+
21+
byte[] bytes = file.getBytes();
22+
23+
System.out.println("File Name: " + file.getOriginalFilename());
24+
System.out.println("File Content Type: " + file.getContentType());
25+
System.out.println("File Content:\n" + new String(bytes));
26+
27+
return (new ResponseEntity<>("Successful", null, HttpStatus.OK));
28+
}
29+
30+
@RequestMapping(path = "/multiplefileupload/", method = RequestMethod.POST)
31+
public ResponseEntity<String> processFile(@RequestParam("files") List<MultipartFile> files) throws IOException {
32+
33+
for (MultipartFile file : files) {
34+
byte[] bytes = file.getBytes();
35+
36+
System.out.println("File Name: " + file.getOriginalFilename());
37+
System.out.println("File Content Type: " + file.getContentType());
38+
System.out.println("File Content:\n" + new String(bytes));
39+
}
40+
41+
return (new ResponseEntity<>("Successful", null, HttpStatus.OK));
42+
}
43+
}

0 commit comments

Comments
 (0)