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

java 实现读取本地日志文件列表并在浏览器上显示的更多相关文章

  1. nodejs读取本地txt文件并输出到浏览器

    var fs = require('fs'); var chrome=""; //同步执行 function tongbu(){ var data =fs.readFileSync ...

  2. JAVA读取本地html文件里的html文本

    /** * 读取本地html文件里的html代码 * @param file File file=new File("文件的绝对路径") * @return */ public s ...

  3. jmeter读取本地CSV文件

    用jmeter录制考试上传成绩等脚本时,出现的问题及解决方法如下: 1.beanshell前置处理器,不能读取本地csv文件里的数据: 方法一: 在beanshell里不能直接从本地的csv文件里读取 ...

  4. Java学习-042-获取目录文件列表(当前,级联)

    以下三个场景,在我们日常的测试开发中经常遇到: 软件自动化测试,在进行参数测试时,我们通常将所有相似功能的参数文件统一放在一个目录中,在自动化程序启动的时候,获取资源参数文件夹中所有参数文件,然后解析 ...

  5. 读取本地json文件,并转换为dictionary

    // 读取本地JSON文件 - (NSDictionary *)readLocalFileWithName:(NSString *)name { // 获取文件路径 NSString *path = ...

  6. jQuery ajax读取本地json文件

    jQuery ajax读取本地json文件 json文件 { "first":[ {"name":"张三","sex": ...

  7. node.js读取到的文件列表

    var fs = require('fs'); var path = require('path'); //解析需要遍历的文件夹,我这以E盘根目录为例 var filePath = path.reso ...

  8. JavaScript读取本地json文件

    JavaScript读取本地json文件 今天调试了一上午,通过jQuery读取本地json文件总是失败,始终找不出原因,各种方法都试了 开始总以为是不是json格式的问题.高了半天不行 后来读了一个 ...

  9. 读取本地json文件,转出为指定格式json 使用Base64进行string的加密和解密

    读取本地json文件,转出为指定格式json   引用添加Json.Net 引用命名空间 using Newtonsoft.Json //读取自定目录下的json文件StreamReader sr = ...

  10. Bootstrap做的HTML页面在本地IE打开正常,放到服务器上显示就不正常了

    <meta name="renderer" content="webkit"> <meta http-equiv="X-UA-Com ...

随机推荐

  1. WPFMVVM实现ICommand与INotifyPropertyChanged接口

    话不多说直接上代码 INotifyPropertyChanged实现代码 internal class NotifyBase : INotifyPropertyChanged { public eve ...

  2. ES6判断对象是否为空

    1.ES6判断对象是否为空{} let obj = {} if(Object.keys(obj).length == 0){ console.log("对象是空的") }else{ ...

  3. nginx重启报错 nginx: [error] open() “/run/nginx.pid“ failed (2: No such file or directory)

    原文: https://blog.csdn.net/CRMEB/article/details/120779362 nginx [error] open() "/usr/local/var/ ...

  4. 通过ESP8266WiFi模块调用“心知天气”接口 获取天气信息

    在分析代码之前,首先介绍 ArduinoJson 库的安装及"心知天气"的ID申请 一.安装 ArduinoJson 库 进入 Arduino 开发环境后,选择菜单栏-->工 ...

  5. openframeworks 设置不显示控制台窗口

    1.设置Linker 下的System->选择Windows 2.设置入口函数,然后编译就可以了 int APIENTRY wWinMain(_In_ HINSTANCE hInstance, ...

  6. jdk 1.8 处理 map和list

    1.map 的 key 和 value 反转 2. list<Object> 取两列转成  map<String,String> map 3. list<Object&g ...

  7. php functions 生成唯一码

    <?php /** * 微擎密码生成 * */ function we7password($passwordinput, $salt, $authkey) { $passwordinput = ...

  8. 一个因为windows系统缺失文件而导致的pyspark的BUG

    背景: 在windows 系统中开发pyspark程序. 一个简单的WC程序: from pyspark.sql import SparkSession spark = SparkSession.bu ...

  9. k8s如何配置secret保存harbor仓库账号密码、pod中怎么使用harbor仓库镜像

    转载: https://blog.csdn.net/MssGuo/article/details/127312239

  10. Net Core 3.1 ONVIF 操控海康摄像头

    先给出实现的代码 https://github.com/lu1770/onvif-client.git 也可以通过安装包来使用功能 dotnet add package Onvif 基本用法 Agen ...