puzhibing
2026-04-01 528aaa61d59d039f185854c31cf91d6995cb068d
修改上传文件类型限制
2个文件已修改
43 ■■■■■ 已修改文件
ruoyi-service/ruoyi-account/src/main/java/com/ruoyi/account/config/FileUploadConfig.java 3 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ruoyi-service/ruoyi-account/src/main/java/com/ruoyi/account/controller/FileController.java 40 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ruoyi-service/ruoyi-account/src/main/java/com/ruoyi/account/config/FileUploadConfig.java
@@ -15,7 +15,8 @@
@ConfigurationProperties(prefix = "file.upload")
public class FileUploadConfig {
    private String accessPath;
    private String allowExt;
    private String imageExt;
    private String docExt;
    private String location;
    private String qrLocation;
}
ruoyi-service/ruoyi-account/src/main/java/com/ruoyi/account/controller/FileController.java
@@ -32,26 +32,25 @@
    @Autowired
    private FileUploadConfig fileUploadConfig;
    @Resource
    private TokenService tokenService;
    @Resource
    private RedisTemplate redisTemplate;
    @ApiOperation(value = "单文件上传", notes = "单文件上传,rename 默认不重命名")
    @PostMapping(value = "upload", headers = "content-type=multipart/form-data")
    public AjaxResult uploadImageMany(@RequestParam(value = "file") MultipartFile mf) throws IOException {
        Long userId = tokenService.getLoginUserApplet().getUserId();
        if(null == userId){
        if (null == userId) {
            return AjaxResult.error("请先登录");
        }
        if (mf.isEmpty()) {
            return AjaxResult.error("请传入文件!");
        }
        String TimeDir =new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        String TimeDir = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        String realPath = fileUploadConfig.getLocation() + TimeDir;
        File file = new File(realPath);
        // 没有目录就创建
@@ -60,14 +59,14 @@
        }
        // 获取文件名称
        String filename = mf.getOriginalFilename();
        if(filename.contains("../")){
        if (filename.contains("../")) {
            filename = filename.replaceAll("\\.\\./", "");
        }
        // 获取文件后缀
        String ext = filename.substring(filename.lastIndexOf(".") + 1);
        // 检查文件类型
        if (!fileUploadConfig.getAllowExt().contains(ext)) {
            return AjaxResult.error("上传文件格式不正确,仅支持" + fileUploadConfig.getAllowExt());
        if (!fileUploadConfig.getImageExt().contains(ext)) {
            return AjaxResult.error("上传文件格式不正确,仅支持" + fileUploadConfig.getImageExt());
        }
        filename = UUID.randomUUID() + "." + ext;
        File targetFile = new File(realPath, filename);//目标文件
@@ -75,21 +74,22 @@
        //传图片一步到位
        mf.transferTo(targetFile);
        //拼接数据
        String imgstr = fileUploadConfig.getAccessPath() + TimeDir +"/"+ filename;
        String imgstr = fileUploadConfig.getAccessPath() + TimeDir + "/" + filename;
        redisTemplate.opsForValue().set("file:" + userId, filename, 1, TimeUnit.HOURS);
        return AjaxResult.success(imgstr);
    }
    @ApiOperation(value = "单文件上传", notes = "单文件上传,rename 默认不重命名")
    @PostMapping(value = "uploadManage", headers = "content-type=multipart/form-data")
    public AjaxResult uploadManage(@RequestParam(value = "file") MultipartFile mf) throws IOException {
        Long userId = tokenService.getLoginUser().getUserid();
        if(null == userId){
        if (null == userId) {
            return AjaxResult.error("请先登录");
        }
        if (mf.isEmpty()) {
            return AjaxResult.error("请传入文件!");
        }
        String TimeDir =new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        String TimeDir = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        String realPath = fileUploadConfig.getLocation() + TimeDir;
        File file = new File(realPath);
        // 没有目录就创建
@@ -98,14 +98,14 @@
        }
        // 获取文件名称
        String filename = mf.getOriginalFilename();
        if(filename.contains("../")){
        if (filename.contains("../")) {
            filename = filename.replaceAll("\\.\\./", "");
        }
        // 获取文件后缀
        String ext = filename.substring(filename.lastIndexOf(".") + 1);
        // 检查文件类型
        if (!fileUploadConfig.getAllowExt().contains(ext)) {
            return AjaxResult.error("上传文件格式不正确,仅支持" + fileUploadConfig.getAllowExt());
        if (!fileUploadConfig.getDocExt().contains(ext)) {
            return AjaxResult.error("上传文件格式不正确,仅支持" + fileUploadConfig.getDocExt());
        }
        filename = UUID.randomUUID() + "." + ext;
        File targetFile = new File(realPath, filename);//目标文件
@@ -113,7 +113,7 @@
        //传图片一步到位
        mf.transferTo(targetFile);
        //拼接数据
        String imgstr = fileUploadConfig.getAccessPath() + TimeDir +"/"+ filename;
        String imgstr = fileUploadConfig.getAccessPath() + TimeDir + "/" + filename;
        redisTemplate.opsForValue().set("file:" + userId, filename, 1, TimeUnit.HOURS);
        return AjaxResult.success(imgstr);
    }
@@ -142,11 +142,11 @@
    @ApiOperation(value = "单文件上传", notes = "单文件上传,rename 默认不重命名")
    @PostMapping(value = "strUpload", headers = "content-type=multipart/form-data")
    public String strUpload(@RequestParam(value = "file") MultipartFile mf,@RequestParam(value = "fileName")String fileName) throws IOException {
    public String strUpload(@RequestParam(value = "file") MultipartFile mf, @RequestParam(value = "fileName") String fileName) throws IOException {
        if (mf.isEmpty()) {
            return "请传入文件!";
        }
        String TimeDir =new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        String TimeDir = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
//        String realPath = "D:\\file\\" + TimeDir;
        String realPath = fileUploadConfig.getLocation() + TimeDir;
        File file = new File(realPath);
@@ -158,7 +158,7 @@
        //开始从源文件拷贝到目标文件
        //传图片一步到位
        mf.transferTo(targetFile);
        String imgstr = fileUploadConfig.getAccessPath() + TimeDir +"/"+ fileName;
        String imgstr = fileUploadConfig.getAccessPath() + TimeDir + "/" + fileName;
        //拼接数据
//        return TimeDir +"/"+ fileName;
        return imgstr;
@@ -182,8 +182,8 @@
        // 获取文件后缀
        String ext = filename.substring(filename.lastIndexOf("."), filename.length());
        // 检查文件类型
        if (!fileUploadConfig.getAllowExt().contains(ext)) {
            return AjaxResult.error("上传文件格式不正确,仅支持" + fileUploadConfig.getAllowExt());
        if (!fileUploadConfig.getImageExt().contains(ext)) {
            return AjaxResult.error("上传文件格式不正确,仅支持" + fileUploadConfig.getImageExt());
        }
        File targetFile = new File(realPath, filename);//目标文件
        //开始从源文件拷贝到目标文件