95 lines
3.7 KiB
Java
95 lines
3.7 KiB
Java
package com.ruoyi.system.controller;
|
||
|
||
import com.ruoyi.common.core.domain.AjaxResult;
|
||
import com.ruoyi.system.domain.Base64UploadVo;
|
||
import com.ruoyi.system.service.IFileUploadService;
|
||
import org.springframework.beans.factory.annotation.Autowired;
|
||
import org.springframework.web.bind.annotation.*;
|
||
import org.springframework.web.multipart.MultipartFile;
|
||
|
||
import java.util.List;
|
||
|
||
@RestController
|
||
@RequestMapping("/system/file")
|
||
public class FileUploadController {
|
||
|
||
@Autowired
|
||
private IFileUploadService fileUploadService;
|
||
|
||
@PostMapping("/upload")
|
||
public AjaxResult uploadFile(@RequestParam("file") MultipartFile file,
|
||
@RequestParam(value = "module", required = false, defaultValue = "common") String module) {
|
||
try {
|
||
String url = fileUploadService.uploadFile(file, module);
|
||
AjaxResult ajax = AjaxResult.success();
|
||
ajax.put("url", url);
|
||
ajax.put("fileName", file.getOriginalFilename());
|
||
return ajax;
|
||
} catch (Exception e) {
|
||
return AjaxResult.error(e.getMessage());
|
||
}
|
||
}
|
||
@PostMapping("/upload-base64")
|
||
public AjaxResult uploadBase64File(@RequestBody Base64UploadVo vo){
|
||
AjaxResult ajax = AjaxResult.success(fileUploadService.uploadBase64File(vo));
|
||
return ajax;
|
||
}
|
||
@PostMapping("/uploads")
|
||
public AjaxResult uploadFiles(@RequestParam("files") List<MultipartFile> files,
|
||
@RequestParam(value = "module", required = false, defaultValue = "common") String module) {
|
||
try {
|
||
List<String> urls = fileUploadService.uploadFiles(files, module);
|
||
AjaxResult ajax = AjaxResult.success();
|
||
ajax.put("urls", urls);
|
||
ajax.put("count", urls.size());
|
||
return ajax;
|
||
} catch (Exception e) {
|
||
return AjaxResult.error(e.getMessage());
|
||
}
|
||
}
|
||
|
||
@PostMapping("/uploadImage")
|
||
public AjaxResult uploadImage(@RequestParam("file") MultipartFile file,
|
||
@RequestParam(value = "module", required = false, defaultValue = "image") String module) {
|
||
try {
|
||
String originalFilename = file.getOriginalFilename();
|
||
if (originalFilename != null) {
|
||
String extension = originalFilename.substring(originalFilename.lastIndexOf(".") + 1).toLowerCase();
|
||
String[] allowedExtensions = {"png", "jpg", "jpeg", "gif", "bmp", "webp"};
|
||
boolean isImage = false;
|
||
for (String allowed : allowedExtensions) {
|
||
if (allowed.equalsIgnoreCase(extension)) {
|
||
isImage = true;
|
||
break;
|
||
}
|
||
}
|
||
if (!isImage) {
|
||
return AjaxResult.error("只允许上传图片文件(png, jpg, jpeg, gif, bmp, webp)");
|
||
}
|
||
}
|
||
|
||
String url = fileUploadService.uploadFile(file, module);
|
||
AjaxResult ajax = AjaxResult.success();
|
||
ajax.put("url", url);
|
||
ajax.put("fileName", file.getOriginalFilename());
|
||
return ajax;
|
||
} catch (Exception e) {
|
||
return AjaxResult.error(e.getMessage());
|
||
}
|
||
}
|
||
|
||
@DeleteMapping("/delete")
|
||
public AjaxResult deleteFile(@RequestParam("filePath") String filePath) {
|
||
try {
|
||
boolean result = fileUploadService.deleteFile(filePath);
|
||
if (result) {
|
||
return AjaxResult.success("删除成功");
|
||
} else {
|
||
return AjaxResult.error("删除失败,文件不存在或已被删除");
|
||
}
|
||
} catch (Exception e) {
|
||
return AjaxResult.error(e.getMessage());
|
||
}
|
||
}
|
||
}
|