支持附件上传

rf
liming 2023-12-10 20:31:12 +08:00
parent aa18507143
commit 904728ef59
4 changed files with 75 additions and 1 deletions

View File

@ -0,0 +1,50 @@
package com.ruoyi.web.controller.common;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.exception.NonCaptureException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.framework.config.ServerConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
@RestController
@RequestMapping("/file")
public class FileUploadController {
private final Logger log = LoggerFactory.getLogger(FileUploadController.class);
@Autowired
private ServerConfig serverConfig;
@Anonymous
@PostMapping("/upload")
@SuppressWarnings("DuplicatedCode")
public AjaxResult uploadFile(MultipartFile file) {
try {
log.info("文件 {} 上传中...", file.getOriginalFilename());
// 上传文件路径
String filePath = RuoYiConfig.getUploadPath();
// 上传并返回新文件名称
String fileName = FileUploadUtils.upload(filePath, file);
String url = serverConfig.getUrl() + fileName;
AjaxResult ajax = AjaxResult.success();
ajax.put("url", url);
ajax.put("fileName", fileName);
ajax.put("newFileName", FileUtils.getName(fileName));
ajax.put("originalFilename", file.getOriginalFilename());
log.info("文件 {} 上传成功!", file.getOriginalFilename());
return ajax;
} catch (Exception e) {
throw new NonCaptureException(StringUtils.format("文件 {} 上传失败!", file.getOriginalFilename()), e);
}
}
}

View File

@ -0,0 +1,13 @@
package com.ruoyi.common.exception;
/**
*
*
* @author liming
* @date 2023/12/10 20:11
*/
public class NonCaptureException extends RuntimeException {
public NonCaptureException(String message, Throwable cause) {
super(message, cause);
}
}

View File

@ -1,6 +1,8 @@
package com.ruoyi.framework.web.exception;
import javax.servlet.http.HttpServletRequest;
import com.ruoyi.common.exception.NonCaptureException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.access.AccessDeniedException;
@ -135,4 +137,13 @@ public class GlobalExceptionHandler
{
return AjaxResult.error("演示模式,不允许操作");
}
/**
*
*/
@ExceptionHandler(NonCaptureException.class)
public AjaxResult handleNonCaptureException(NonCaptureException e)
{
throw e;
}
}

View File

@ -499,7 +499,7 @@ export const selectComponents = [
__slot__: {
'list-type': true
},
action: 'https://jsonplaceholder.typicode.com/posts/',
action: 'http://localhost:8080/file/upload',
disabled: false,
accept: '',
name: 'file',