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. 【学习总结】快速上手Linux玩转典型应用-第2章-linux简介

    课程目录链接 快速上手Linux玩转典型应用-目录 目录 1. 什么是Linux 2. Linux能够做什么事情 3. Linux的学习方法 4. 忘掉Windows的所有东西 1. 什么是Linux ...

  2. Schematic - "DesignEntry 30 ERROR Instance referencing symbol is out of date"

    Schematic - "DesignEntry 30 ERROR Instance referencing symbol is out of date"     Descript ...

  3. 判断用户输入YES或NO

    #!bin/bash#作者:liusingbon#功能:判断用户输入的是 Yes 或 NOread -p "Are you sure?[y/n]:" surecase $sure ...

  4. STM32 常用词汇释义

    1.AF——Alternate function   复用功能: 2.NVIC——Nested Vectored Interrupt Controller   内嵌向量中断控制器 3.ISER[8]— ...

  5. Linux之vim按键

    1. 移动光标的方法 h或左箭头 光标向左移动一个字符 j或下箭头 光标向下移动一个字符 k或上箭头 光标向上移动一个字符 l或右箭头 光标向右移动一个字符 如果想要向下移动30行,可以使用“30j” ...

  6. java Byte源码分析

    源码: public static int toUnsignedInt(byte x) { return ((int) x) & 0xff; } 原理: -128(byte) 原码:10000 ...

  7. 【宝藏】题解(五校联考3day1)

    分析 如果打爆搜的话可以拿60分. 首先知道期望是可以累加的,即i通过j去到k的期望,等于i去到j的期望加j去到k的期望. 所以令d[i]表示i的出度,F[i]表示从i到i的父亲的期望,G[i]表示i ...

  8. Shell-03

    Shell-03 编程原理 编程介绍 最开始的编程 机械码(16进制)—CPU会识别 计算机只能识别二进制指令 程序 = 指令 + 数据 驱动: 硬件默认是不能使用的 驱动程序----不同的厂家硬件设 ...

  9. 重塑云上的 Java 语言

    音乐无国界,但是音乐人有国界. 云原生亦如此.虽没有限定的编程语言,但应用所使用的编程语言已经决定了应用部署运行的行为. Java 诞生于20年前,拥有大量优秀的企业级框架,践行 OOP 理念,更多体 ...

  10. spring,配置文件applictionContext.xml,Mybatis mybatis.xml,springMVC spring整合springMVC mybatis