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();
            }
        }
    }
    

      

  • 相关阅读:
    Matlab 绘制三维立体图(以地质异常体为例)
    Azure DevOps的variable group实现array和hashtable参数的传递
    Azure DevOps 利用rest api设置variable group
    Azure AADSTS7000215 其中一种问题的解决
    Power BI 实现实时更新Streaming Dataset
    AAD Service Principal获取azure user list (Microsoft Graph API)
    Matlab 沿三维任意方向切割CT图的仿真计算
    Azure Powershell script检测登陆并部署ARM Template
    Azure KeyVault设置策略和自动化添加secrets键值对
    Azure登陆的两种常见方式(user 和 service principal登陆)
  • 原文地址:https://www.cnblogs.com/yanqin/p/8526675.html
Copyright © 2011-2022 走看看