JAVA在页面查看或下载服务器上的日志
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在页面查看或下载服务器上的日志的更多相关文章
- Android开发之下载服务器上的一张图片到本地java代码实现HttpURLConnection
package com.david.HttpURLConnectionDemo; import java.io.FileOutputStream; import java.io.IOException ...
- 怎么把html页面部署到云服务器上
1,下载nginx 2,把页面放置到云服务器上 3,通过配置nginx conf下的nginx.conf文件,就可以通过ip:port访问到了 链接:https://www.cnblogs.com/f ...
- flask中下载服务器上特定路径的文件
使用flask下载服务器上某个路径下的文件 path:文件路径以及需要下载的文件,直接写入参数有安全隐患,实际应用中需要判断权限之类的 from flask import send_file, mak ...
- linux服务器上nginx日志访问量统计命令
linux服务器上nginx日志访问量统计命令 日志文件所在地方:/var/log/nginx/access_iqueendress.com.log/var/log/nginx/access_m.iq ...
- Java如何检查文件是否在服务器上被修改了?
在Java编程中,如何检查文件是否在服务器上被修改了? 以下示例显示如何检查文件是否在服务器上进行了修改. package com.yiibai; import java.net.URL; impor ...
- zoc 下载服务器上数据出现的问题
zoc上有一个download的按钮能够将服务器上的数据下载下来,但是在下载过程中出现了错误 但是点击会出现下面的问题: 这个配置默认是采用ZMODEM文件传输协议 但是可以看出来,这个传输协议有点问 ...
- linux下载服务器上的文件命令-sz
语法:sz 文件 比如要下载下面这个com.zip这个压缩包 输入sz com.zip 弹出下载页面,即可开始下载文件
- Linux服务器上创建日志服务器和FTP服务器
参考地址: http://www.111cn.net/sys/CentOS/81133.htm https://www.cnblogs.com/laoxiajiadeyun/p/9943742.htm ...
- Java使用Sftp实现对跨服务器上传、下载、打包、写入相关操作
1.Maven引入jar <dependency> <groupId>com.jcraft</groupId> <artifactId>jsch< ...
随机推荐
- 吴恩达机器学习7:代价函数(Cost function)
一.简介 1.在线性回归中,我们有一个这样的训练集,M代表训练样本的数量,假设函数即用来进行预测的函数是这样的线性函数的形式,我们接下来看看怎么选择这两个参数: 2.如下图中,怎么选择两个参数来更好的 ...
- Linux系统性能测试工具(六)——磁盘io性能工具之dd
本文介绍关于Linux系统(适用于centos/ubuntu等)的磁盘io性能测试工具-dd.磁盘io性能测试工具包括: fio: dd
- Linux学习--第三天--linux文件目录、ls、mkdir、mv、rm、touch、cat、tac、more、less、head、tail、ln、chmod、chown、chgrp、umask
文件目录 目录名 备注 bin 下面的命令所有人都可以运行 sbin 只有root才能运行,s代表super /mnt,/media,/misc 都是挂载目录,但一般只用mnt /opt 第三方软件安 ...
- 通过virsh console进入虚拟机
1.virsh启动一个虚拟机.执行脚本test_qga.sh 2.virsh vncdisplay <vm_ID> 3.vnc登录到vm里面,执行#systemctl start seri ...
- VCL界面控件DevExpress VCL Controls发布v18.2.7|附下载
DevExpress VCL Controls是 Devexpress公司旗下最老牌的用户界面套包.所包含的控件有:数据录入,图表,数据分析,导航,布局,网格,日程管理,样式,打印和工作流等,让您快速 ...
- Git之仓库管理
介绍以及安装: Git 是一个开源的分布式版本控制软件,用以有效.高速的处理从很小到非常大的项目版本管理. Git 最初是由Linus Torvalds设计开发的,用于管理Linux内核开发.Git ...
- [洛谷P4438] HNOI2018 道路
问题描述 W 国的交通呈一棵树的形状.W 国一共有n - 1个城市和n个乡村,其中城市从1到n - 1 编号,乡村从1到n编号,且1号城市是首都.道路都是单向的,本题中我们只考虑从乡村通往首都的道路网 ...
- [CF852D] Exploration plan
问题描述 The competitors of Bubble Cup X gathered after the competition and discussed what is the best w ...
- HTML Tag, 把 WCAG 的标准和语义网的目标进行代码上的体现
1. 文档声明:<!Doctype> 其实这跟 WCAG 根本上连不上什么直接关系,但为了一个兼容性更好,特别是向后兼容的页面,我推荐你这样写: <!Doctype html> ...
- 浙大PAT CCCC L3-015 球队“食物链” ( 搜索 && 剪枝 )
题目链接 题意 : 有 n 个球队,给出主客场胜负图,找出一个序列 1.2.3..... 使得 1 战胜过 2 .2 战胜过 3.3 战胜过 4..... n 战胜过 1 ( 这个序列是 1~n 的其 ...