• 网页输出日志文件


    网页输出日志文件

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

      

  • 相关阅读:
    遇到缺陷错误时,有耐心,冷静思考,保证大方向是正确的
    学习新芯片和做新的工作内容时 DEMO
    599密码
    shop--7.店铺编辑和列表--店铺管理 前端
    shop--7.店铺编辑和列表--店铺列表展示 前端
    shop--7.店铺编辑和列表--店铺列表 分页查询,模糊查询--service层,controller层实现
    shop--7.店铺编辑和列表--店铺列表 分页查询,模糊查询--dao层实现
    shop--7.店铺编辑和列表--更新店铺的信息 前端实现 js
    shop--7.店铺编辑和列表--更新店铺的信息,包括对店铺照片的处理,根据shopId获取shop信息
    shop--7.店铺编辑和列表--联合查询的结果集封装ResultMap association collection
  • 原文地址:https://www.cnblogs.com/yanqin/p/8526675.html
Copyright © 2020-2023  润新知