upd
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
package com.flower.common;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/upload")
|
||||
public class UploadController {
|
||||
private final String uploadDir;
|
||||
|
||||
public UploadController(@Value("${app.upload.dir}") String uploadDir) {
|
||||
this.uploadDir = uploadDir;
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
public ApiResponse<Map<String, String>> upload(@RequestParam("file") MultipartFile file) {
|
||||
String bashPath = "D:\\bs\\flower\\files";
|
||||
if (file == null || file.isEmpty()) {
|
||||
throw new ApiException(400, "请选择文件");
|
||||
}
|
||||
String original = file.getOriginalFilename();
|
||||
String suffix = "";
|
||||
if (original != null && original.contains(".")) {
|
||||
suffix = original.substring(original.lastIndexOf("."));
|
||||
}
|
||||
String filename = UUID.randomUUID().toString().replace("-", "") + suffix;
|
||||
Path dir = Paths.get(uploadDir);
|
||||
try {
|
||||
Files.createDirectories(dir);
|
||||
Path path = dir.resolve(filename);
|
||||
file.transferTo(path.toFile());
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
throw new ApiException(500, "上传失败");
|
||||
}
|
||||
Map<String, String> result = new HashMap<>();
|
||||
result.put("url", "/uploads/" + filename);
|
||||
return ApiResponse.ok(result);
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.flower.confession;
|
||||
package com.flower.confession;
|
||||
|
||||
import com.flower.common.ApiException;
|
||||
import com.flower.common.ApiResponse;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
server:
|
||||
port: 8080
|
||||
port: 8088
|
||||
|
||||
spring:
|
||||
datasource:
|
||||
|
||||
Reference in New Issue
Block a user