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


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

posted on 2023-01-18 17:36  hiyonx  阅读(219)  评论(0编辑  收藏  举报

导航