网页输出日志文件

网页输出日志文件

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

  

转载于:https://www.cnblogs.com/yanqin/p/8526675.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值