committed by
Gitee
4 changed files with 75 additions and 1 deletions
@ -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); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -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); |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue