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>
相关推荐
马船长25 分钟前
RCE-PLUS (学习记录)
java·linux·前端
Ares码农人生28 分钟前
React 高级组件开发:动态逻辑与性能优化
vue.js·前端框架
HelloZheQ31 分钟前
深入了解 Java 字符串:基础、操作与性能优化
java·python·性能优化
魔法工坊42 分钟前
只谈C++11新特性 - 删除函数
java·开发语言·c++
学前端的小朱1 小时前
修改输出资源的名称和路径、自动清空上次打包资源
前端·webpack·打包工具
小跳不会Coding1 小时前
uniapp通过v-if进行判断时,会出现闪屏?【已解决】
uni-app
嘤嘤怪呆呆狗1 小时前
【开发问题记录】执行 git cz 报require() of ES Module…… 错误
前端·javascript·vue.js·git·vue
落霞与孤鹭齐飞。。1 小时前
学生考勤系统|Java|SSM|VUE| 前后端分离
java·mysql·毕业设计·课程设计
橙子家czzj1 小时前
关于 K8s 的一些基础概念整理-补充【k8s系列之二】
java·开发语言·kubernetes
云:2 小时前
寒假准备找实习复习java基础-day1
java·开发语言