当前位置: 首页 > news >正文

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>
http://www.lryc.cn/news/127190.html

相关文章:

  • NeuralNLP-NeuralClassifier的使用记录(二),训练预测自己的【中文文本多分类】
  • express学习笔记8 - 文件上传 下载以及预览
  • Python系统学习1-9-类(一)
  • 什么是公网、私网、内网、外网?
  • 一篇文章教会你搭建私人kindle图书馆,并内网穿透实现公网访问
  • 好用的安卓手机投屏到mac分享
  • df -h
  • 彻底卸载Android Studio
  • QT 5.12配置OpenCV3.4.10
  • Qt应用开发(基础篇)——选项卡窗口 QTabWidget
  • Socks5代理在多线程爬虫中的应用
  • 机器学习笔记:主动学习(Active Learning)初探
  • linux github 仓库管理常用操作
  • IT运维:使用数据分析平台监控深信服防火墙
  • 深入解析 Axios Blob 的使用方法及技巧
  • 爬虫逆向实战(十三)--某课网登录
  • 4.SpringCloud
  • OLED透明屏采购指南:如何选择高质量产品?
  • 机器学习编译系列
  • MySQL 数据库巡检系统的设计与应用
  • 工程项目管理系统源码+功能清单+项目模块+spring cloud +spring boot em
  • 前端笔试+面试分享
  • C Language Handout
  • 数据结构——栈(C语言)
  • Linux 内核内存管理 page_address 函数
  • macOS使用ffmpeg与QT进行音视频推拉流
  • ARTS打卡:双指针的尝试
  • JavaWeb-DAO设计模式
  • 重温git和GitHub
  • C# WPF 中 外部图标引入iconfont,无法正常显示问题 【小白记录】