首页 > 编程语言 >java 实现读取本地日志文件列表并在浏览器上显示

java 实现读取本地日志文件列表并在浏览器上显示

时间:2023-01-18 17:45:16浏览次数:39  
标签:sb 浏览器 String file import java 日志 final append


import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.*;
import java.net.MalformedURLException;
import java.net.URLEncoder;
import java.nio.file.Path;
import java.nio.file.Paths;

@RestController
@RequestMapping("file")
public class FileController1 {

    private static final String directory = "D:/";

    /**
     * 获取文件列表
     */
    @GetMapping("files")
    public ResponseEntity<?> list(@RequestParam(required = false) String filename) throws Exception {
        final File file = new File(directory, filename == null ? "" : filename);
        if (file.isDirectory()) {
            return ResponseEntity.ok().contentType(MediaType.TEXT_HTML).body(buildHtml(file));
        } else {
            if (file.length() > 5 * 1024 * 1024) {
                final String name = file.getName();
                return ResponseEntity.ok().header(HttpHeaders.CONTENT_DISPOSITION,
                        "attachment;filename*=UTF-8''" + URLEncoder.encode(name, "UTF-8")).body(download(file));
            } else {
                BufferedReader br = new BufferedReader(new FileReader(file));
                final StringBuilder sb = new StringBuilder();
                String st;
                while ((st = br.readLine()) != null) {
                    sb.append(st).append("\n");
                }
                return ResponseEntity.ok().header("Content-Type", "text/plain;charset=UTF-8").body(sb.toString());
            }
        }
    }

    private Resource download(File file) throws MalformedURLException {
        final String parent = file.getParent();
        final String name = file.getName();
        final Path path = Paths.get(parent).resolve(name);
        return new UrlResource(path.toUri());
    }

    public String buildHtml(File file) {
        final StringBuilder sb = new StringBuilder("<!DOCTYPE html><html lang='zh'><head><meta charset='UTF-8'></head><body><ul>");
        final File[] files = file.listFiles();
        final String parent = file.getParent();
        if (parent != null) {
            final String url = parent.substring(directory.length()).replaceAll("[\\\\/]", "%5C");
            sb.append("<li><a href='/file/files?filename=").append(url).append("'>· ·</a></li>");
        }
        if (files != null) {
            for (File file1 : files) {
                final String filePath = file1.getPath().substring(directory.length());
                final String fileUrl = filePath.replaceAll("[\\\\/]", "%5C");
                sb.append("<li><a href='/file/files?filename=").append(fileUrl).append("'>").append(file1.getName()).append("</a></li>");
            }
        }
        sb.append("</ul></body></html>");
        return sb.toString();
    }
}

标签:sb,浏览器,String,file,import,java,日志,final,append
From: https://www.cnblogs.com/hiyonx/p/17060307.html

相关文章

  • Java RMI机制
    概念RMI机制即Java远程方法调用(JavaRemoteMethodInvocation),在Java语言中,一种用于实现远程过程调用的应用程序编程接口。它使得客户端上运行的程序可以远程调用远程服务......
  • java 405_Http状态405-方法不允许
    解决方法:删除下列代码。super.doGet(req.resp);super.doPost(req.resp);分析:405错误一般指请求methodnotallowed错误。请求行中指定的请求方法不能被用于请求响应......
  • ASP.NET Log4Net日志的配置及使用,文件写入 Global.asax设置读取log4net.config 配置
    ASP.NETLog4Net日志的配置及使用,文件写入Global.asax设置读取log4net.config配置文件https://www.cnblogs.com/Hmd528/p/11082814.htmlLog4net是Apachelog4j框架在......
  • Pure JavaScript Stars Generator All In One
    PureJavaScriptStarsGeneratorAllInOnepadStart&padEnd//constrating=stars=>`★★★★★☆☆☆☆☆`.slice(5-stars,10-stars);//constrating......
  • MS SQL Server 日志审核工具
    手动审核数据库活动是一项艰巨的任务。有效实现这一目标的最佳方法是使用全面的解决方案来简化和自动化数据库和活动监控。该解决方案还应使数据库管理员能够监控、跟踪、即......
  • 使用vector采集nginx日志,并输出为prometheus_exporter
    日志示例{"remote_addr":"10.43.144.171","@timestamp":"2023-01-17T17:27:14+08:00","request":"GET/v1/stat_des/?id=20230117170221HTTP/1.1","status":"200","size......
  • vector采集k8s日志
    安装helmcurl-xproxy.ops.qianxin-inc.cn:3128-Ohttps://get.helm.sh/helm-v3.8.0-linux-amd64.tar.gzsudomvhelm/usr/bin/添加vector存储仓库sudohelmre......
  • 浏览器彻底清缓存
    以google浏览器为例。1.清除浏览器缓存执行图中步骤或使用快捷键ctrl+shift+delete将【基本】和【高级】的时间范围修改为时间不限,点击【清除数据】即可2.清除缓......
  • 千锋JavaScript学习笔记
    千锋JavaScript学习笔记目录千锋JavaScript学习笔记写在前面1.JS基础1.1变量1.2数据类型1.3数据类型转换1.4运算符1.5条件1.6循环1.7函数1.8对象数据类型1.9数......
  • Java8时间段分组
    根据统计的时间段进行分组,例如当天的时间段0点到6点、6点到12点,12点到18点的统计数量,这时候繁杂的for循环会导致代码量激增,切不够明了。我们可以用Java8的链式方式来进行分......