zoukankan      html  css  js  c++  java
  • 网页输出日志文件

    网页输出日志文件

    @Controller
    @RequestMapping("/query/{username}/{password}")
    public class QueryController {
        private HttpServletRequest request;
        private HttpServletResponse response;
        private String username;
        private String password;
        final String userdir = System.getProperty("user.dir");
        final String logPath = userdir.replace("bin", "logs");
    
        @ModelAttribute
        public void init(@PathVariable String username,@PathVariable String password,HttpServletRequest request,HttpServletResponse response){
            this.username = username;
            this.password = password;
            this.request = request;
            this.response = response;
        }
    
        private void outFile(String path){
            File file = new File(path);
            try(
                    InputStreamReader read = new InputStreamReader(new FileInputStream(file),"UTF-8");
                    BufferedReader br=new BufferedReader(read)
            ) {
                StringBuilder sb = new StringBuilder();
                htmlPrefix(sb);
                String r = br.readLine();
                while (r != null) {
                    sb.append("<pre>" + r + "</pre>");
                    r = br.readLine();
                }
                htmlSuffix(sb);
                responseOut(response, sb.toString());
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        @RequestMapping("/logFile")
        @ResponseBody
        public String queryLogFile(String filename) {
            if(checkUser()){
                if(filename !=null) {
                    String path = logPath + "/" + filename;
                    outFile(path);
                }
            }
            return null;
        }
    
        @RequestMapping("/log")
        @ResponseBody
        public String queryLog(){
            if(checkUser()) {
                StringBuilder sb = new StringBuilder();
                htmlPrefix(sb);
                File file = new File(logPath);
                File[] list = file.listFiles();
                for (File file1 : list) {
                    if (file1.isFile())
                        sb.append("<a href="logFile?filename=" + file1.getName() + "" target="_blank">" + file1.getName() + "</a><br/>");
                }
                htmlSuffix(sb);
                return sb.toString();
            }
            return null;
        }
    
        @RequestMapping("/loggerFile")
        @ResponseBody
        public String queryLoggerFile(String dir,String filename)  {
            if(checkUser()){
                if(dir!=null && filename !=null) {
                    String path = userdir + "/" + dir + "/" + filename;
                    outFile(path);
                }
            }return null;
        }
    
        /**
         * 查询 logger插件生成的目录
         * @return
         */
        @RequestMapping("/logger")
        @ResponseBody
        public String queryLogger(@RequestParam(required = false) String dir){
            if(checkUser()) {
                StringBuilder sb = new StringBuilder();
                htmlPrefix(sb);
                String path = userdir;
                if(dir !=null){
                    path = path +"/"+dir;
                }
                File file = new File(path);
                File[] list = file.listFiles();
                for (File file1 : list) {
                    if(file1.isDirectory())
                        sb.append("<a href="logger?dir="+file1.getName()+"">"+file1.getName()+"</a><br/>");
                }
                if(dir!=null) {
                    for (File file1 : list) {
                        if (file1.isFile())
                            sb.append("<a href="loggerFile?dir=" + dir + "&filename=" + file1.getName() + "" target="_blank">" + file1.getName() + "</a><br/>");
                    }
                }
                htmlSuffix(sb);
                return sb.toString();
            }
            return null;
        }
    
        private void htmlPrefix(StringBuilder sb){
            sb.append("<!DOCTYPE html>");
            sb.append("<html lang="zh-CN">");
            sb.append("<head>");
            sb.append("<meta charset="utf-8">");
            sb.append("<title>LOGGER</title>");
            sb.append("</head>");
            sb.append("<body>");
        }
        private void htmlSuffix(StringBuilder sb){
            sb.append("</body>");
            sb.append("</html>");
        }
    
        /**
         * 验证
         * @return
         */
        private boolean checkUser(){
            if(username!=null && password !=null){
                if(username.equals("user") && password.equals("password")){
                    return true;
                }
            }
            return false;
        }
    
        /**
         * 输出文字
         *
         * @param response
         * @param s
         */
        private static void responseOut(HttpServletResponse response, String s) {
            response.setContentType("text/html;charset=UTF-8");
            response.setCharacterEncoding("UTF-8");
            try (
                    PrintWriter pw = response.getWriter()
            ) {
                pw.write(s);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    

      

  • 相关阅读:
    Java--分布式系统高并发解决方案
    Eclipse 快捷键
    Java--基础命名空间
    Java--发送邮件
    Java--垃圾收集算法及内存分配策略
    Java--Vector类
    第四篇 express 安装esasticsearch
    第三篇elasticsearch分布式安装
    第二篇elasticsearch配置
    elasticsearch介绍
  • 原文地址:https://www.cnblogs.com/yanqin/p/8526675.html
Copyright © 2011-2022 走看看