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

若依 ruoyi-vue 接口挂载获取Resources静态资源文件权限校验

解决小程序图片打包过大,放置后端,不引用ngnix、minio等组件,还能进行权限校验

在这里插入图片描述

package com.huida.web.controller.common.app;import com.huida.common.core.controller.BaseController;
import com.huida.common.utils.file.FileUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import javax.servlet.http.HttpServletResponse;
import java.io.IOException;@RestController
@RequiredArgsConstructor
@RequestMapping("/static")
public class StaticResourcesController extends BaseController {@Autowiredprivate ResourceLoader resourceLoader;@GetMapping("/img/train/{fileName}")public void getImgTrain(@PathVariable String fileName, HttpServletResponse response) throws IOException {Resource resource = resourceLoader.getResource("classpath:static/img/train/" + fileName);if (resource.exists()) {response.setContentType(FileUtils.getContentType(resource.getFilename()));FileUtils.writeBytes(resource.getURI().getPath(), response.getOutputStream());}}@GetMapping("/img/{fileName}")public void getImg(@PathVariable String fileName, HttpServletResponse response) throws IOException {Resource resource = resourceLoader.getResource("classpath:static/img/" + fileName);if (resource.exists()) {response.setContentType(FileUtils.getContentType(resource.getFilename()));FileUtils.writeBytes(resource.getURI().getPath(), response.getOutputStream());}}}
  /*** 根据文件名,设置HttpServletResponse的ContentType** @param fileName 文件名* @return 结果* @author weimingzhong* @date 2024/4/3 9:50*/public static String getContentType(String fileName) {String fileExtension = getFileExtension(fileName);switch (fileExtension) {case "png":return MediaType.IMAGE_PNG_VALUE;case "jpg":case "jpeg":return MediaType.IMAGE_JPEG_VALUE;case "gif":return MediaType.IMAGE_GIF_VALUE;case "pdf":return MediaType.APPLICATION_PDF_VALUE;// Add more cases for other file types if neededdefault:return MediaType.APPLICATION_OCTET_STREAM_VALUE; // Default to octet-stream}}private static String getFileExtension(String fileName) {int dotIndex = fileName.lastIndexOf(".");if (dotIndex > 0 && dotIndex < fileName.length() - 1) {return fileName.substring(dotIndex + 1).toLowerCase();} else {return "";}}
http://www.lryc.cn/news/333546.html

相关文章:

  • 【STM32嵌入式系统设计与开发】——16InputCapture(输入捕获应用)
  • 「论文阅读」还在手写Prompt,自动Prompt搜索超越人类水平
  • 安全测试概述和用例设计
  • JavaScript 超详细学习思路
  • LeetCode:1483. 树节点的第 K 个祖先(倍增 Java)
  • ConstraintLayout在复杂布局中,出现卡顿问题解决记录
  • 责任链模式详解+代码案例
  • 如何让Webots支持C#语言开发的控制器
  • 如何将本地仓库放到远程仓库中
  • Jedis-事务
  • 智慧安防监控EasyCVR视频调阅和设备录像回看无法自动播放的原因排查与解决
  • 百元不入耳蓝牙耳机哪个好?必入五款高性价比产品
  • android APP monkey 测试
  • IMBoy缓存系统深度解析:为何选择depcache而非ETS或Redis
  • Twitter Api查询用户粉丝列表
  • 深入理解计算机系统 家庭作业 2.96
  • 主函数if __name__ == ‘__main__‘:
  • 34.Python从入门到精通—Python3 正则表达式检索和替换
  • springboot 反射调用ServiceImpl时报错:java.lang.NullPointerExceptio、,mapper为null【解决方法】
  • 内网安全之域内密码喷洒
  • 何为HTTP状态码?一文清楚基本概念。
  • SV学习笔记(七)
  • Windows SDK(五)按钮静态文本与编辑框控件
  • 基于SSM框架实现的在线心理评测与咨询系统(技术栈 spring+springmvc+mybatis+jsp+jquery+css)
  • GD32F470_ DS18B20温度传感器模块移植
  • 【JAVASE】带你了解instanceof和equals的魅力
  • 【Linux】进程控制详解
  • Mysql 高性能的sql优化方案和建议
  • 鸿蒙实战开发:【实现应用悬浮窗】
  • 应用开发:python解析斗鱼弹幕