Skip to content

Commit

Permalink
support to save the uploaded file (#29)
Browse files Browse the repository at this point in the history
* support to save the uploaded file

* update readme

---------

Co-authored-by: rick <[email protected]>
  • Loading branch information
LinuxSuRen and LinuxSuRen authored Oct 3, 2024
1 parent 6872d45 commit 4072902
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ Run with Maven command:
mvn spring-boot:run
```

Change the listen port:
```shell
java -jar demo.jar --server.port=8081
```

## OpenAPI definition
You can visit it via: http://localhost:8080/v3/api-docs

Expand Down
10 changes: 8 additions & 2 deletions src/main/java/io/github/devopsws/demo/service/FileService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package io.github.devopsws.demo.service;

import java.io.FileOutputStream;

import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
Expand All @@ -16,8 +19,11 @@ public Message<?> upload(@RequestParam("file") MultipartFile file) {
System.out.println("Received file uploading request");
Message<String> message = new Message<String>();
if (!file.isEmpty()) {
try {
System.out.println("Uploading file size:" + file.getSize());
String filename = file.getOriginalFilename();
try (FileOutputStream out = new FileOutputStream(filename)) {
System.out.println("Uploading file size: " + file.getSize() + ", name: " + filename);

FileCopyUtils.copy(file.getInputStream(), out);

message.setMessage("ok");
} catch (Exception e) {
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/io/github/devopsws/demo/service/RootService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package io.github.devopsws.demo.service;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController("/")
public class RootService {

@GetMapping("")
public String index() {
return "Let's learn spring boot!";
}
}

0 comments on commit 4072902

Please sign in to comment.