1.配置

FileUtils类所需jar包的maven地址

<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.2</version>
</dependency>

2.代码

参数为日志文件的相对路径

package com.example.demo.io;

import org.apache.commons.io.FileUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.net.URLEncoder; /**
* @author Sue
* @create 2019-05-08 10:03
**/
@Controller
public class LogUtil {
/**
* 读取txt文件的内容
*
* @param file 想要读取的文件对象
* @return 返回文件内容
*/
public static String txt2String(File file) {
StringBuilder result = new StringBuilder();
try {
//构造一个BufferedReader类来读取文件
BufferedReader br = new BufferedReader(new FileReader(file));
String s = null;
//使用readLine方法,一次读一行
while ((s = br.readLine()) != null) {
result.append(System.lineSeparator() + s);
}
br.close();
} catch (Exception e) {
e.printStackTrace();
}
return result.toString();
} @GetMapping("/getLog")
public void main(HttpServletRequest request, HttpServletResponse response,String theFileName) {
String property = System.getProperty("user.dir");
String absolutePath = property + File.separator + theFileName;
File file = new File(absolutePath); // File file = new File("D:\\logback.2019-04-29.log");
System.out.println(txt2String(file) + ">");
try {
response.getWriter().write(txt2String(file));
} catch (IOException e) {
e.printStackTrace();
}
} @GetMapping("/download")
public ResponseEntity<byte[]> downLoad(HttpServletRequest request,String theFileName) throws Exception, FileNotFoundException {
HttpHeaders headers = new HttpHeaders();
String property = System.getProperty("user.dir");
String absolutePath = property + File.separator + theFileName;
File file = new File(absolutePath);
// File file = new File("D:\\logback.2019-04-29.log");
String fileName = file.getName();
if (file.exists()) {
//下载显示的文件名,解决中文名称乱码问题
String userAgent = request.getHeader("user-agent").toLowerCase();
String downloadFielName; if (userAgent.contains("msie") || userAgent.contains("like gecko")) {
downloadFielName = URLEncoder.encode((fileName), "UTF-8");
} else {
downloadFielName = new String((fileName).getBytes("UTF-8"), "iso-8859-1");
}
headers.set(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + downloadFielName);
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); } else {
throw new FileNotFoundException("文件不存在");
}
return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file), headers, HttpStatus.OK); }
}

JAVA在页面查看或下载服务器上的日志的更多相关文章

  1. Android开发之下载服务器上的一张图片到本地java代码实现HttpURLConnection

    package com.david.HttpURLConnectionDemo; import java.io.FileOutputStream; import java.io.IOException ...

  2. 怎么把html页面部署到云服务器上

    1,下载nginx 2,把页面放置到云服务器上 3,通过配置nginx conf下的nginx.conf文件,就可以通过ip:port访问到了 链接:https://www.cnblogs.com/f ...

  3. flask中下载服务器上特定路径的文件

    使用flask下载服务器上某个路径下的文件 path:文件路径以及需要下载的文件,直接写入参数有安全隐患,实际应用中需要判断权限之类的 from flask import send_file, mak ...

  4. linux服务器上nginx日志访问量统计命令

    linux服务器上nginx日志访问量统计命令 日志文件所在地方:/var/log/nginx/access_iqueendress.com.log/var/log/nginx/access_m.iq ...

  5. Java如何检查文件是否在服务器上被修改了?

    在Java编程中,如何检查文件是否在服务器上被修改了? 以下示例显示如何检查文件是否在服务器上进行了修改. package com.yiibai; import java.net.URL; impor ...

  6. zoc 下载服务器上数据出现的问题

    zoc上有一个download的按钮能够将服务器上的数据下载下来,但是在下载过程中出现了错误 但是点击会出现下面的问题: 这个配置默认是采用ZMODEM文件传输协议 但是可以看出来,这个传输协议有点问 ...

  7. linux下载服务器上的文件命令-sz

    语法:sz  文件 比如要下载下面这个com.zip这个压缩包 输入sz com.zip 弹出下载页面,即可开始下载文件

  8. Linux服务器上创建日志服务器和FTP服务器

    参考地址: http://www.111cn.net/sys/CentOS/81133.htm https://www.cnblogs.com/laoxiajiadeyun/p/9943742.htm ...

  9. Java使用Sftp实现对跨服务器上传、下载、打包、写入相关操作

    1.Maven引入jar <dependency> <groupId>com.jcraft</groupId> <artifactId>jsch< ...

随机推荐

  1. python学习笔记(2):科学计算及数据可视化入门

    一.NumPy 1.NumPy:Numberical Python 2.高性能科学计算和数据分析的基础包 3.ndarray,多维数组(矩阵),具有矢量运算的能力,快速.节省空间 (1)ndarray ...

  2. 【React -- 5/100】 组件复用

    组件复用 React组件复用概述 思考:如果两个组件中的部分功能相似或相同,该如何处理? 处理方式:复用相似的功能 复用什么? state 操作state的方法 两种方式: render props模 ...

  3. BrokenPipeError: [Errno 32] Broken pipe

    运行Pytorch tutorial代码报错:BrokenPipeError: [Errno 32] Broken pipe 源代码地址: Training a classifier (CIFAR10 ...

  4. python multiprocessing模块

    python multiprocessing模块 原文地址 multiprocessing multiprocessing支持子进程.通信和共享数据.执行不同形式的同步,提供了Process.Queu ...

  5. Spring基础02——Spring HelloWorld

    1.首先我们来创建一个HelloWorld类,通过Spring来对这个类进行实例化 package com.wzy.lesson1; /** * @author wzy * @version 1.0 ...

  6. VMware Workstation key

    VMware workstation 11 Pro key VY790-6FF9M-H8D3Q-1PY5T-YFUTD AA5M8-8NGD3-M805Y-K5Q7G-X28E6 UY3TR-88W4 ...

  7. 013-linux系统管理——系统资源查看

    linux系统管理——系统资源查看 ############# vmstat 命令 监控系统资源 ############# vmstat [刷新时间 刷新次数] [root@zabbix ~]# v ...

  8. windows 10 安装openssh 0x800f0954 的一种解决方法

    安装与卸载参考:https://docs.microsoft.com/zh-cn/windows-server/administration/openssh/openssh_install_first ...

  9. spring bean的生命周期与作用域

    bean的作用域 singleton:单例模式,Spring IoC容器中只会存在一个共享的Bean实例,无论有多少个Bean引用它,始终指向同一对象. prototype:原型模式,每次通过Spri ...

  10. 安装win10笔记

    1.使用pe安装的时候,要利用winNTSetup安装 2. 3.引导和安装驱动器都选择c盘 4.版本选择教育版,专业版photoshop 不好使.