当前位置: 首页 > news >正文

山东大学软件学院项目实训-基于大模型的模拟面试系统-专栏管理部分

本周我的主要任务是关于专栏管理部分的完善。

专栏图片的显示问题

问题分析

在这里插入图片描述
在这里插入图片描述

根据代码可知:图片URL来自于portfolio.headImgUrl,而且如果URL不存在的话,应该显示的是无图片,而网页中显示加载失败说明portfolio.headImgUrl应该是存在的,但是路径可能有问题。在控制台中的404报错也印证了这一点。
在这里插入图片描述

对其中的URL进行解码可以得到http://localhost:3000/portfolio/上传失败!,看来是图片在上传时就有问题,导致URL被拼接成了上传失败。
在这里插入图片描述
在后端进行DeBug调试时找到问题所在,原来是配置文件中的存储图片的目录的路径有问题,导致返回了一个上传失败。

问题解决

通过修改配置文件中的路径使其能够显示正常的路径而不是上传失败,但现在路径仍不是我们想要的路径,所以还需要将存到数据库中的图片路径逻辑进行修改。
在这里插入图片描述
现在图片已经能够正确存储在后端的static文件夹下,并且其URL正确的存储到了数据库中。

后端

然后需要找到前端向后端请求的函数:

fetchPostDetail({ commit }, params = {}) {
    // const delay = fetchDelay(
    //   isBrowser
    // )
    // if (isBrowser) {
    //   Vue.nextTick(() => {
    //     window.scrollTo(0, 300);
    //   })
    // }

    if (typeof params.portfolio_id === 'undefined') {
      commit('updateDetailData', getDefaultListData())
      return;
    }
    commit('updateDetailFetching', true)
    // commit('updateDetailData', {})
    return this.$axios
      .$get(`${PORTFOLIO_API_PATH}/detail/${params.portfolio_id}`)
      .then(response => {
        return new Promise(resolve => {
          commit('updateDetailData', response)
          commit('updateDetailFetching', false)
          resolve(response)
          // delay(() => {
          //   resolve(response)
          // })
        })
      })
      .catch(error => {
        commit('updateDetailFetching', false)
        return Promise.reject(error)
      })
  }

上述函数通过调用后端接口(${PORTFOLIO_API_PATH}/detail/${params.portfolio_id})获取作品集详情数据,更新到 Vuex store 的detail.data中,而作品集封面的URL就在detail.data中。

添加前端请求图片的接口并以base64编码的形式返回。

    @GetMapping("/image/{idPortfolio}/base64")
    public GlobalResult<String> getPortfolioImageAsBase64(@PathVariable Long idPortfolio, @RequestParam(defaultValue = "0") Integer type) throws IOException {
        // Get the portfolio DTO which contains the image path
        PortfolioDTO portfolioDTO = portfolioService.findPortfolioDTOById(idPortfolio, type);

        if (portfolioDTO == null || portfolioDTO.getHeadImgUrl() == null) {
            throw new FileNotFoundException("Portfolio or image not found");
        }
        String path = portfolioDTO.getHeadImgUrl().replace("src/main/resources/", "");
        String extension = path.substring(path.lastIndexOf(".") + 1);
        // 3. 确定MIME类型
        String mimeType;
        switch (extension) {
            case "png":  mimeType = "image/png"; break;
            case "jpg":
            case "jpeg": mimeType = "image/jpeg"; break;
            case "gif":  mimeType = "image/gif"; break;
            case "svg":  mimeType = "image/svg+xml"; break;
            case "webp": mimeType = "image/webp"; break;
            default:     mimeType = "application/octet-stream";
        }
        ClassPathResource resource = new ClassPathResource(path);
        // Read file content and encode as base64
        try (InputStream inputStream = resource.getInputStream()) {
            byte[] fileContent = IOUtils.toByteArray(inputStream);
            String base64 = Base64.getEncoder().encodeToString(fileContent);
            String res = "data:" + mimeType + ";base64," + base64;
            GlobalResult<String> result = GlobalResultGenerator.genSuccessResult("success");
            result.setData(res);
            return result;
        } catch (IOException e) {
            throw new RuntimeException("加载图片失败: " + path, e);
        }

    }
前端

添加该函数单独获取base64编码的图片。

    async fetchImageAsBase64() {
     if(this.portfolio?.headImgUrl?.startsWith('data:image')){
       return;
     }
     try {
       if (!this.portfolio?.headImgUrl) return;
       if (this.portfolio.headImgUrl.startsWith('data:image')) {
         return this.portfolio.headImgUrl;
       }
       
       const response = await this.$axios.$get(
         `/api/portfolio/image/${this.portfolio.idPortfolio}/base64`,
         { responseType: 'text' }
       );
       this.imgUrl = response;
       console.log(this.imgUrl)
     } catch (error) {
       console.error('获取Base64图片失败:', error);
       return null; // 或返回占位图URL
     }
   }
 },

相关文章:

  • 代码随想录算法训练营Day27
  • vulkanscenegraph显示倾斜模型(5.9)-vsg中vulkan资源的编译
  • 多模态大语言模型arxiv论文略读(十三)
  • 【使用jenkins+docker自动化部署java项目】
  • MacOs下解决远程终端内容复制并到本地粘贴板
  • Web渗透之文件包含漏洞
  • 分层对象模型:PO、DTO、VO、BO定义区别与使用场景
  • win10中快速访问部分外网的快捷设置方法
  • 【已更新完毕】2025泰迪杯数据挖掘竞赛B题数学建模思路代码文章教学:基于穿戴装备的身体活动监测
  • MySQL中的隐式转换和显式转换
  • 2025认证杯挑战赛B题【 谣言在社交网络上的传播 】原创论文讲解(含完整python代码)
  • java学习总结(if switch for)
  • Go:方法
  • 【Java】查看当前 Java 使用的垃圾回收器
  • 【GitHub探索】mcp-go,MCP协议的Golang-SDK
  • AVL搜索树
  • 互斥锁(mutex) ---- 静态锁与动态锁
  • (C语言)算法复习总结1——二分查找
  • 【T2I】Region-Aware Text-to-Image Generation via Hard Binding and Soft Refinement
  • GPT-2 语言模型 - 模型训练
  • 颜福庆与顾临的争论:1930年代在中国维持一家医学院要花多少钱
  • 王毅会见巴西外长维埃拉、总统首席特别顾问阿莫林
  • AI观察|从万元到百万元,DeepSeek一体机江湖混战
  • 男子退机票被收票价90%的手续费,律师:虽然合规,但显失公平
  • 商务部就开展打击战略矿产走私出口专项行动应询答记者问
  • 重庆一高校75万采购市价299元产品?工作人员:正在处理