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

前端工程

访问方式

powershell 复制代码
http://127.0.0.1:8080/context/frontEnd/index

放行

java 复制代码
public class SecurityConfig extends WebSecurityConfigurerAdapter {
"/frontEnd/**",

SysFrontEndController

java 复制代码
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

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>
相关推荐
pianmian13 小时前
类(JavaBean类)和对象
java
我叫小白菜4 小时前
【Java_EE】单例模式、阻塞队列、线程池、定时器
java·开发语言
Albert Edison4 小时前
【最新版】IntelliJ IDEA 2025 创建 SpringBoot 项目
java·spring boot·intellij-idea
daols884 小时前
vue vxe-table 自适应列宽,根据内容自适应宽度的2种使用方式
vue.js·vxe-table
超级小忍5 小时前
JVM 中的垃圾回收算法及垃圾回收器详解
java·jvm
weixin_446122465 小时前
JAVA内存区域划分
java·开发语言·redis
小小小小宇5 小时前
虚拟列表兼容老DOM操作
前端
悦悦子a啊5 小时前
Python之--基本知识
开发语言·前端·python
勤奋的小王同学~5 小时前
(javaEE初阶)计算机是如何组成的:CPU基本工作流程 CPU介绍 CPU执行指令的流程 寄存器 程序 进程 进程控制块 线程 线程的执行
java·java-ee
TT哇5 小时前
JavaEE==网站开发
java·redis·java-ee