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>
相关推荐
gnip27 分钟前
企业级配置式表单组件封装
前端·javascript·vue.js
一只叫煤球的猫1 小时前
写代码很6,面试秒变菜鸟?不卖课,面试官视角走心探讨
前端·后端·面试
excel2 小时前
Three.js 材质(Material)详解 —— 区别、原理、场景与示例
前端
掘金安东尼3 小时前
抛弃自定义模态框:原生Dialog的实力
前端·javascript·github
皮皮林5513 小时前
IDEA 源码阅读利器,你居然还不会?
java·intellij idea
hj5914_前端新手6 小时前
javascript基础- 函数中 this 指向、call、apply、bind
前端·javascript
薛定谔的算法6 小时前
低代码编辑器项目设计与实现:以JSON为核心的数据驱动架构
前端·react.js·前端框架
Hilaku7 小时前
都2025年了,我们还有必要为了兼容性,去写那么多polyfill吗?
前端·javascript·css
yangcode7 小时前
iOS 苹果内购 Storekit 2
前端
LuckySusu7 小时前
【js篇】JavaScript 原型修改 vs 重写:深入理解 constructor的指向问题
前端·javascript