Skip to content

Commit 03c5e4c

Browse files
committed
Added Example Application For FileUpload using Spring RestTemplated
1 parent b8c78d9 commit 03c5e4c

3 files changed

Lines changed: 85 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: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.baeldung.web.upload.client;
2+
3+
4+
import org.springframework.core.io.FileSystemResource;
5+
import org.springframework.core.io.Resource;
6+
import org.springframework.http.HttpEntity;
7+
import org.springframework.http.HttpHeaders;
8+
import org.springframework.http.MediaType;
9+
import org.springframework.http.ResponseEntity;
10+
import org.springframework.util.LinkedMultiValueMap;
11+
import org.springframework.util.MultiValueMap;
12+
import org.springframework.web.client.RestTemplate;
13+
14+
import java.io.IOException;
15+
import java.nio.file.Files;
16+
import java.nio.file.Path;
17+
18+
public class MultipartFileUploadClient {
19+
20+
public static void main(String[] args) throws IOException {
21+
MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
22+
HttpHeaders headers = new HttpHeaders();
23+
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers);
24+
25+
body.add("file", getTestFile());
26+
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
27+
28+
String serverUrl = "http://localhost:8080/fileserver/";
29+
RestTemplate restTemplate = new RestTemplate();
30+
ResponseEntity<String> response = restTemplate.postForEntity(serverUrl, requestEntity, String.class);
31+
System.out.println("Response code: " + response.getStatusCode());
32+
}
33+
34+
public static Resource getTestFile() throws IOException {
35+
Path testFile = Files.createTempFile("test-file", ".txt");
36+
System.out.println("Creating and Uploading Test File: " + testFile);
37+
Files.write(testFile, "Hello World !!, This is a test file.".getBytes());
38+
return new FileSystemResource(testFile.toFile());
39+
}
40+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
13+
@RestController
14+
@RequestMapping("/fileserver")
15+
public class FileServerResource {
16+
17+
@RequestMapping(method = RequestMethod.POST)
18+
public ResponseEntity<String> processFile(@RequestParam("file") MultipartFile multipartFile) throws IOException {
19+
20+
byte[] bytes = multipartFile.getBytes();
21+
22+
System.out.println("File Name: " + multipartFile.getOriginalFilename());
23+
System.out.println("File Content Type: " + multipartFile.getContentType());
24+
System.out.println("File Content:\n" + new String(bytes));
25+
26+
return (new ResponseEntity<>("Successful", null, HttpStatus.OK));
27+
}
28+
}

0 commit comments

Comments
 (0)