1.添加头像上传接口
This commit is contained in:
@@ -1,10 +1,12 @@
|
||||
package vvpkassistant;
|
||||
import org.dromara.x.file.storage.spring.EnableFileStorage;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.scheduling.annotation.EnableAsync;
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableAsync
|
||||
@EnableFileStorage
|
||||
public class Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
@@ -392,7 +392,7 @@ public class UserController {
|
||||
return ResponseData.success(userService.addUserWithMail(model));
|
||||
}
|
||||
|
||||
@GetMapping("/activateAccount")
|
||||
@GetMapping("/activate")
|
||||
public ResponseData<Object> activateAccount(@RequestParam("token") String token){
|
||||
return ResponseData.success(userService.activateAccount(token));
|
||||
}
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
package vvpkassistant.file.controller;
|
||||
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import vvpkassistant.Data.ResponseData;
|
||||
import vvpkassistant.file.service.FileService;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
/*
|
||||
* @author: ziin
|
||||
* @date: 2025/8/7 13:58
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("file")
|
||||
public class FileController {
|
||||
|
||||
|
||||
@Resource
|
||||
private FileService fileService;
|
||||
|
||||
|
||||
@PostMapping("/uploadHeadIcon")
|
||||
public ResponseData<Object> uploadHeadIcon(@RequestParam("file") MultipartFile file) {
|
||||
return ResponseData.success(fileService.uploadHeadIcon(file));
|
||||
}
|
||||
|
||||
}
|
||||
11
src/main/java/vvpkassistant/file/service/FileService.java
Normal file
11
src/main/java/vvpkassistant/file/service/FileService.java
Normal file
@@ -0,0 +1,11 @@
|
||||
package vvpkassistant.file.service;
|
||||
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
/*
|
||||
* @author: ziin
|
||||
* @date: 2025/8/7 13:58
|
||||
*/
|
||||
public interface FileService {
|
||||
Object uploadHeadIcon(MultipartFile file);
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package vvpkassistant.file.service;
|
||||
|
||||
import cn.hutool.core.lang.UUID;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.dromara.x.file.storage.core.FileInfo;
|
||||
import org.dromara.x.file.storage.core.FileStorageService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
/*
|
||||
* @author: ziin
|
||||
* @date: 2025/8/7 13:58
|
||||
*/
|
||||
@Service
|
||||
@Slf4j
|
||||
public class FileServiceImpl implements FileService {
|
||||
|
||||
@Resource
|
||||
private FileStorageService fileStorageService;//注入实列
|
||||
|
||||
@Override
|
||||
public Object uploadHeadIcon(MultipartFile file) {
|
||||
// 生成 UUID 文件名,保留原扩展名
|
||||
String originalFilename = file.getOriginalFilename();
|
||||
String extension = originalFilename != null && originalFilename.contains(".")
|
||||
? originalFilename.substring(originalFilename.lastIndexOf("."))
|
||||
: "";
|
||||
String uuidFileName = UUID.randomUUID().toString().replace("-", "") + extension;
|
||||
|
||||
// 使用 fileStorageService 进行上传
|
||||
// 设置保存的文件名为 UUID
|
||||
|
||||
// 返回文件信息或 URL
|
||||
|
||||
log.info("Uploading head icon to file: {}", uuidFileName);
|
||||
return fileStorageService.of(file)
|
||||
.setSaveFilename(uuidFileName) // 设置保存的文件名为 UUID
|
||||
.upload();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user