• 基于RuoYi-Flowable-Plus的若依ruoyi-nbcio支持本地图片上传与回显的功能实现(一)


         

    更多ruoyi-nbcio功能请看演示系统

    gitee源代码地址

    前后端代码: https://gitee.com/nbacheng/ruoyi-nbcio

    演示地址:RuoYi-Nbcio后台管理系统

          原先这个基于RuoYi-Flowable-Plus的这个不支持本地图片上传,只支持oss图片上传,所以需要增加相应的本地上传图片功能。

        1、先要理解原先若依的本地图片上传与显示的过程

    图片上传
    现在想要去上传一张照片,首先前端调用上传接口

    /**
     * xx图片上传
     */
    @PostMapping("/avatar")
    public AjaxResult avatar(@RequestParam("avatarfile") MultipartFile file) throws IOException
    {
        if (!file.isEmpty()){
            // ...
            String avatar = FileUploadUtils.upload(RuoYiConfig.getAvatarPath(), file);
            if (userService.updateUserAvatar(loginUser.getUsername(), avatar))
            {
                AjaxResult ajax = AjaxResult.success();
                ajax.put("imgUrl", avatar);
                // ...
                return ajax;
            }
        }
        return AjaxResult.error("上传图片异常,请联系管理员");
    }

    保存到数据库,并返回给前端

    {
        code: 200
        imgUrl: "/profile/avatar/2023/10/11/nbcio_20231011222512A001.png"
        msg: "操作成功"
    }

    web前端将其拼接,就可以访问到服务器上的本地文件
    http://localhost/dev-api//profile/avatar/2023/10/11/nbcio_20231011222512A001.png

    图片路径
    前端
    可以看到图片路径有点陌生,这里使用到了代理;路径首先被web前端解析

    -- 前端配置
    process.env.VUE_APP_BASE_API = 'http://localhost/dev-api'

    -- 使用代理来解决跨域问题
    http://localhost/dev-api -> http://localhost:8080

    -- 解析前端请求 /dev-api
    http://localhost/dev-api/profile/avatar/2023/10/11/nbcio_20231011222512A001.png
    -- 此时,再将请求交给后端处理
    http://localhost:8080/profile/avatar/2023/10/11/nbcio_20231011222512A001.png

    后端
    后端对匹配的URL进行拦截 /profile/** ,映射至本地文件夹 RuoYiConfig.getProfile()。

    /**
     * 通用配置
     * 
     * @author ruoyi
     */
    @Configuration
    public class ResourcesConfig implements WebMvcConfigurer
    {
        /** 配置静态资源映射 */
        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry)
        {
            /** 本地文件上传路径 */
            registry.addResourceHandler(Constants.RESOURCE_PREFIX + "/**").addResourceLocations("file:" + RuoYiConfig.getProfile() + "/");
            // ...
        }
        // ...
    }

    相关常量

    # 资源映射路径 前缀
    Constants.RESOURCE_PREFIX = "/profile"

    # RuoYiConfig.getProfile() 获取项目信息 ruoyi.profile
    /home/nbcio/upload
     

    这样图片数据便被从本地拿到,经历了 前端 -> 后端 -> 本地文件 的过程!

    2、根据上面的一些原理,现在思路修改一下,我的已经去掉/dev-api,同时也取消什么代理这些

    接下来一步一步进行修改

    3、application.yml增加下面内容,主要是两项与上传文件相关的内容

    1. # 项目相关配置
    2. ruoyi:
    3. # 名称
    4. name: RuoYi-Nbcio
    5. # 版本
    6. version: ${ruoyi-nbcio.version}
    7. # 版权年份
    8. copyrightYear: 2023
    9. # 实例演示开关
    10. demoEnabled: true
    11. # 本地:local\Minio:minio\阿里云:alioss
    12. uploadtype: local
    13. #文件上传根目录 设置
    14. profile: /home/nbcio/upload
    15. # 获取ip地址开关
    16. addressEnabled: true
    17. # 缓存懒加载
    18. cacheLazy: false

       4、application-dev.ym 先增加下面一项

    1. nbcio:
    2. localfilehttp: http://localhost:9060 #上传图片的http基地址

      5、ResourcesConfig.java修改如下:

    1. package com.ruoyi.framework.config;
    2. import com.ruoyi.common.config.RuoYiConfig;
    3. import com.ruoyi.common.constant.Constants;
    4. import com.ruoyi.framework.interceptor.PlusWebInvokeTimeInterceptor;
    5. import org.springframework.context.annotation.Bean;
    6. import org.springframework.context.annotation.Configuration;
    7. import org.springframework.web.cors.CorsConfiguration;
    8. import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
    9. import org.springframework.web.filter.CorsFilter;
    10. import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
    11. import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
    12. import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
    13. /**
    14. * 通用配置
    15. *
    16. * @author Lion Li, nbacheng
    17. */
    18. @Configuration
    19. public class ResourcesConfig implements WebMvcConfigurer {
    20. @Override
    21. public void addInterceptors(InterceptorRegistry registry) {
    22. // 全局访问性能拦截
    23. registry.addInterceptor(new PlusWebInvokeTimeInterceptor());
    24. }
    25. @Override
    26. public void addResourceHandlers(ResourceHandlerRegistry registry) {
    27. /** 本地文件上传路径 */
    28. registry.addResourceHandler(Constants.RESOURCE_PREFIX + "/**")
    29. .addResourceLocations("file:" + RuoYiConfig.getProfile() + "/");
    30. }
    31. /**
    32. * 跨域配置
    33. */
    34. @Bean
    35. public CorsFilter corsFilter() {
    36. CorsConfiguration config = new CorsConfiguration();
    37. config.setAllowCredentials(true);
    38. // 设置访问源地址
    39. config.addAllowedOriginPattern("*");
    40. // 设置访问源请求头
    41. config.addAllowedHeader("*");
    42. // 设置访问源请求方法
    43. config.addAllowedMethod("*");
    44. // 有效期 1800秒
    45. config.setMaxAge(1800L);
    46. // 添加映射路径,拦截一切请求
    47. UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
    48. source.registerCorsConfiguration("/**", config);
    49. // 返回新的CorsFilter
    50. return new CorsFilter(source);
    51. }
    52. }

  • 相关阅读:
    华为 - HCNA 笔记
    LVS:NAT模式详解
    windows 2016 故障转移集群 副节点报错 群集网络名称资源“群集名称”注册一个或多个相关联的 DNS 名称失败
    linux篇【6】:进程等待
    Django框架学习大纲
    SpringMVC
    【数据结构与算法】之深入解析“网格游戏”的求解思路和算法示例
    ble理论(14) ble扫描详解
    AVS3中的intra string copy(ISC)
    【精灵雪花特效】(HTML+JS+CSS+代码+效果)
  • 原文地址:https://blog.csdn.net/qq_40032778/article/details/133780345