zoukankan      html  css  js  c++  java
  • SpringBoot自定义实现跳转页面

    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.stereotype.Controller;
    import org.springframework.util.ClassUtils;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.*;
    
    @Controller
    public class PageController {
        private static ClassLoader classLoader = ClassUtils.getDefaultClassLoader();
        private static final Logger log = LoggerFactory.getLogger(PageController.class);
    
        @RequestMapping("/")
        public void root(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            request.getRequestDispatcher("/index.html").forward(request, response);
        }
    
        @RequestMapping("**.html")
        public void page(HttpServletRequest request, HttpServletResponse response) throws IOException {
            String servletPath = request.getServletPath();
            log.info(request.getMethod() + " " + servletPath);
            String filePath = "templates" + servletPath;

            InputStream inputStream = classLoader.getResourceAsStream(filePath);
            if (inputStream == null) {
                response.sendError(404);
                return;
            }
            response.setContentType("text/html; charset=utf-8");

            OutputStream outputStream = response.getOutputStream();
            int readChar;
            while ((readChar = inputStream.read()) != -1) {
                outputStream.write(readChar);
            }
            outputStream.flush();
            inputStream.close();
        }
    }
  • 相关阅读:
    应用网络电视机顶盒通过宽带网络代替数字电视
    该公众号暂时无法提供服务请稍后再试
    有点坑爹的GDALComputeRasterMinMax函数
    微信硬件平台框架说明及接入流程
    微信硬件设备接入接口协议
    微信思维
    百度SEO建议
    公众平台调整SSL安全策略,请开发者注意升级
    微信支付开发教程
    .net文件分片上传,断点续传
  • 原文地址:https://www.cnblogs.com/gjq1126-web/p/12614870.html
Copyright © 2011-2022 走看看