网页输出日志文件

网页输出日志文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
@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();
        }
    }
}

  

posted @   抱明月  阅读(1265)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示