Skip to content

Commit 4072902

Browse files
authored
support to save the uploaded file (#29)
* support to save the uploaded file * update readme --------- Co-authored-by: rick <[email protected]>
1 parent 6872d45 commit 4072902

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ Run with Maven command:
1313
mvn spring-boot:run
1414
```
1515

16+
Change the listen port:
17+
```shell
18+
java -jar demo.jar --server.port=8081
19+
```
20+
1621
## OpenAPI definition
1722
You can visit it via: http://localhost:8080/v3/api-docs
1823

src/main/java/io/github/devopsws/demo/service/FileService.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package io.github.devopsws.demo.service;
22

3+
import java.io.FileOutputStream;
4+
5+
import org.springframework.util.FileCopyUtils;
36
import org.springframework.web.bind.annotation.PostMapping;
47
import org.springframework.web.bind.annotation.RequestMapping;
58
import org.springframework.web.bind.annotation.RequestParam;
@@ -16,8 +19,11 @@ public Message<?> upload(@RequestParam("file") MultipartFile file) {
1619
System.out.println("Received file uploading request");
1720
Message<String> message = new Message<String>();
1821
if (!file.isEmpty()) {
19-
try {
20-
System.out.println("Uploading file size:" + file.getSize());
22+
String filename = file.getOriginalFilename();
23+
try (FileOutputStream out = new FileOutputStream(filename)) {
24+
System.out.println("Uploading file size: " + file.getSize() + ", name: " + filename);
25+
26+
FileCopyUtils.copy(file.getInputStream(), out);
2127

2228
message.setMessage("ok");
2329
} catch (Exception e) {
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package io.github.devopsws.demo.service;
2+
3+
import org.springframework.web.bind.annotation.GetMapping;
4+
import org.springframework.web.bind.annotation.RestController;
5+
6+
@RestController("/")
7+
public class RootService {
8+
9+
@GetMapping("")
10+
public String index() {
11+
return "Let's learn spring boot!";
12+
}
13+
}

0 commit comments

Comments
 (0)