springboot工程集成前端编译包,用于uni-app webView工程,解决其需独立部署带来的麻烦,场景如页面->画布->图片->pdf

news/2025/3/19 11:19:03/

前端工程

在这里插入图片描述

访问方式

http://127.0.0.1:8080/context/frontEnd/index

放行

public class SecurityConfig extends WebSecurityConfigurerAdapter {
"/frontEnd/**",

SysFrontEndController

import lombok.extern.slf4j.Slf4j;
import nl.basjes.shaded.org.springframework.core.io.ClassPathResource;
import nl.basjes.shaded.org.springframework.util.AntPathMatcher;
import org.springframework.http.MediaType;
import org.springframework.http.MediaTypeFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.HandlerMapping;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.charset.Charset;
import java.util.Optional;/*** @author 蓝之静云* @date 2023-08-10 */
@RestController
@RequestMapping("/frontEnd")
@Slf4j
public class SysFrontEndController {private AntPathMatcher antPathMatcher = new AntPathMatcher();@RequestMapping("/**")public void getStaticResource(HttpServletRequest request, HttpServletResponse response) throws IOException {// 获取完整的路径String uri = (String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);// 获取映射的路径String pattern = (String) request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE);String customPath = antPathMatcher.extractPathWithinPattern(pattern, uri);if ("index".equals(customPath)) customPath = customPath + ".html";// 按/截取String[] split = customPath.split("/");// 最后一个就是文件名String filename = split[split.length - 1];// 若是图片资源if (filename.contains(".png")|| filename.contains(".jpg")|| filename.contains(".ico")|| filename.contains(".gif")|| filename.contains(".svg")|| filename.contains(".pdf")|| filename.contains(".jpeg")) {ServletOutputStream outputStream = null;InputStream inputStream = null;try {ClassPathResource classPathResource = new ClassPathResource("static/" + customPath);inputStream = classPathResource.getInputStream();response.setContentType("image/" + filename.split("\\.")[1]);outputStream = response.getOutputStream();int len;byte[] buffer = new byte[4096];while ((len = inputStream.read(buffer)) != -1) {outputStream.write(buffer, 0, len);}outputStream.flush();} catch (Exception e) {e.printStackTrace();} finally {outputStream.close();inputStream.close();}} else {InputStream stream = Thread.currentThread().getContextClassLoader().getResourceAsStream("static/" + customPath);assert stream != null;ByteBuffer buf = ByteBuffer.allocate(stream.available());Channels.newChannel(stream).read(buf);buf.flip();String fileStr = Charset.defaultCharset().decode(buf).toString();buf.clear();Optional<MediaType> mediaType = MediaTypeFactory.getMediaType(filename);mediaType.ifPresent(type -> response.setContentType(type.toString() + ";charset=UTF-8"));response.setCharacterEncoding("UTF-8");response.getWriter().write(fileStr);}}
}

pom.xml

		<resources><resource><directory>src/main/java</directory><includes><include>**/*.xml</include></includes></resource><resource><directory>src/main/resources</directory><includes><include>**/*.*</include></includes></resource><resource><directory>src/main/webapp</directory><targetPath>META-INF/resources</targetPath><includes><include>**/*.*</include></includes></resource></resources>

http://www.ppmy.cn/news/1021562.html

相关文章

Mac OS键盘常用快捷键

图形按键⌘Command 键⌃Control 键⌥Option 键⇧Shift 键⇪Caps Lockfn功能键 常用快捷键剪切、拷贝和粘贴 您可以在大多数 app 中使用这些快捷键来剪切、拷贝或粘贴选中的项目。其中包括文本、图片、音乐等等。您甚至可以在 Finder 中拷贝和粘贴文件&#xff0c;来将文件拷贝到…

仿到位|独立版家政上门预约服务小程序家政保洁师傅上门服务小程序上门服务在线派单源码

上门预约服务派单小程序家政 小程序 同城预约 开源代码 独立版. 程序完整,经过安装检测,可放心下载安装。 适合本地的一款上门预约服务小程序,功能丰富,适用多种场景。 程序功能:城市管理/小程序DIY/服务订单/师傅管理/会员卡功能/营销功能/文章功能等等

前端性能优化之性能优化的指标和工具(chrome devtools、lighthouse、webpagetest)

文章目录 引言一、为什么要进行web性能优化二、RAIL测量模型1. 什么是RAIL2. 性能测量工具 三、性能测量工具的使用和性能指标以及优化目标1. Chrome DevTools1. 打开调试工具方式和配置2. network下的几个性能指标1. requests 请求总数2. transferred实际从服务器下载的数据量…

sql高频面试题-去除最高最低的平均

面试或者笔试的过程中会设定各种各样的场景&#xff0c;在这些场景下考查我们SQL的查询能力&#xff0c;但是万变不离其宗&#xff0c;业务场景只是一个表现形式&#xff0c;抽象为SQL问题后其实基本上就是几类问题&#xff1a;计算累计、连续&#xff0c;分类TopN等。只要掌握…

专注于创意设计,为您的小程序和网站建设带来更多的可能性

随着移动互联网的快速发展&#xff0c;越来越多的企业开始关注小程序和网站建设&#xff0c;以此来拓展业务和提升品牌形象。 在这个领域中&#xff0c;创意设计扮演着关键的角色。它不仅可以帮助企业打造独特的形象和品牌&#xff0c;还能够提高用户体验和购买决策的效率。 因…

MySQL到Oracle快速上手

第一次做Oracle项目的时候对一些语法区别不太清楚&#xff0c;这里列出一些开发中发现的与MYSQL不同的点 一个用户相当于一个数据库 表空间 表空间是用于存储表、索引和其他数据库对象的逻辑存储结构。每个表空间由一个或多个数据文件组成&#xff0c;这些文件可以位于不同的物…

背上大书包准备run之CSS篇

时隔一年多又要准备面试嘞。唉&#xff0c;人生呐&#xff0c;真是变幻莫测哟~ 社招应该不会问很多css吧&#xff0c;&#xff0c;&#xff0c;但是应该也会问吧&#xff0c;&#xff0c;&#xff0c;应该是从好多好多问题里只抽一两个问问吧&#x1f62d; 哦还有h5&#xff…

【行为型设计模式】C#设计模式之观察者模式

题目&#xff1a;假设你正在开发一个简单的新闻发布系统&#xff0c;该系统允许用户订阅不同的新闻频道&#xff0c;并在有新闻发布时向订阅者发送通知。使用观察者模式设计和实现该系统。观察者模式的相关概念和定义&#xff1a; 观察者模式是一种行为设计模式&#xff0c;它定…