JAVA在页面查看或下载服务器上的日志
1.配置
FileUtils类所需jar包的maven地址
1 2 3 4 5 | <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version> 2.2 </version> </dependency> |
2.代码
参数为日志文件的相对路径
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 | 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); } } |
本博客文章均已测试验证,欢迎评论、交流、点赞。
部分文章来源于网络,如有侵权请联系删除。
转载请注明原文链接:https://www.cnblogs.com/sueyyyy/p/10831773.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix