Skip to content

Commit

Permalink
support to save the uploaded file
Browse files Browse the repository at this point in the history
  • Loading branch information
LinuxSuRen committed Oct 1, 2024
1 parent 6872d45 commit 60a1fa2
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
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 60a1fa2

Please sign in to comment.