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

前端工程

在这里插入图片描述

访问方式

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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值