相关文章推荐
内向的馒头  ·  5分钟理解什么是多模态 - CSDN博客·  1 周前    · 
内向的馒头  ·  多模态·  1 周前    · 
风流倜傥的烈马  ·  運用網頁比對與文本情感分析之資料隱碼攻擊漏洞 ...·  6 天前    · 
小百科  ›  java输出字符串到多个输出流 同时输出到console终端,网页,文本开发者社区
文本分析
私奔的红烧肉
2 年前
作者头像
用户1258909
0 篇文章

java输出字符串到多个输出流 同时输出到console终端,网页,文本

前往专栏
腾讯云
开发者社区
文档 意见反馈 控制台
首页
学习
活动
专区
工具
TVP
文章/答案/技术大牛
发布
首页
学习
活动
专区
工具
TVP
返回腾讯云官网
社区首页 > 专栏 > 拂晓风起 > java输出字符串到多个输出流 同时输出到console终端,网页,文本

java输出字符串到多个输出流 同时输出到console终端,网页,文本

作者头像
用户1258909
发布 于 2018-07-03 11:57:30
1K 0
发布 于 2018-07-03 11:57:30
举报

网上有不少大牛做了一些比较高级的,例如重写stream类,加入多个输出流。

但其实很多时候我们没必要用到这么复杂。

例如我的应用,我只是想把错误信息输出到网页的同时,简单加几句话,可以把网页上的信息也写一份到数据库或者文本。

之前使用了重定向System.out的做法。

但最近做自定义错误页面的时候,发现一个高手更好的办法。之前都没试过这样用~~~

首先定义一个内存输出流:

ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
PrintStream printStream = new PrintStream(byteArrayOutputStream);

然后过程中,不要用System.out,全部用printStream

到最后,想要输出到哪,就再建立一个PrintStream对象(例如封装FileOutputStream),使用print方法,把byteArrayOutputStream输出。

FileOutputStream fileOutputStream = new FileOutputStream(new File(dir.getAbsolutePath() + File.separatorChar + "error-" + timeStamp + ".txt"));
new PrintStream(fileOutputStream).print(byteArrayOutputStream); //写到文件

最后,贴上完整的error.jsp代码,大家慢慢分析。如果有什么问题,欢迎大家提出指正。

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page isErrorPage="true" %>
<%@page import="java.text.SimpleDateFormat"%>
<%@ page import="java.io.*"%>
    response.setStatus(HttpServletResponse.SC_OK);
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
        <title>错误页面</title>
        <script type="text/javascript" src="./javascript/jquery-1.5.1.min.js"></script>
        <script>
            function showErrorMessage(){
                $("#errorMessageDiv").toggle();
            $(document).ready(showErrorMessage);
        </script>
    </head>
        <table width="100%">
                <td style="border-bottom:dotted 1px Gray;" colspan="2" >
                    <img src="images/error_title_icon.gif" id="img1" />&nbsp;&nbsp;错误提示                               
                </td><td></td>
                <td style="width: 130px" >
                    <img src="images/sorry.gif" id="error_img" />
                <td>尊敬的用户:<br />系统出现了异常,请重试。
                    <br />如果问题重复出现,请向系统管理员反馈。<br /><br />
                    <a id="showErrorMessageButton" href="javascript:showErrorMessage();">详细错误信息</a>
        </table>
        <div id="errorMessageDiv" >
                    try {
                        //全部内容先写到内存,然后分别从两个输出流再输出到页面和文件
                        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                        PrintStream printStream = new PrintStream(byteArrayOutputStream);
                        printStream.println();
                        printStream.println("用户信息");
                        printStream.println("账号:" + request.getSession().getAttribute("userName"));
                        printStream.println("访问的路径: " + request.getAttribute("javax.servlet.forward.request_uri"));
                        printStream.println();
                        printStream.println("异常信息");
                        printStream.println(exception.getClass() + " : " + exception.getMessage());
                        printStream.println();
                        Enumeration<String> e = request.getParameterNames();
                        if (e.hasMoreElements()) {
                            printStream.println("请求中的Parameter包括:");
                            while (e.hasMoreElements()) {
                                String key = e.nextElement();
                                printStream.println(key + "=" + request.getParameter(key));
                            printStream.println();
                        printStream.println("堆栈信息");
                        exception.printStackTrace(printStream);
                        printStream.println();
                        out.print(byteArrayOutputStream);    //输出到网页
                        File dir = new File(request.getRealPath("/errorLog"));
                        if (!dir.exists()) {
                            dir.mkdir();
                        String timeStamp = new SimpleDateFormat("yyyyMMddhhmmssS").format(new Date());
                        FileOutputStream fileOutputStream = new FileOutputStream(new File(dir.getAbsolutePath() + File.separatorChar + "error-" + timeStamp + ".txt"));
                        new PrintStream(fileOutputStream).print(byteArrayOutputStream); //写到文件
                    } catch (Exception ex) {
                        ex.printStackTrace();
 
推荐文章
内向的馒头  ·  5分钟理解什么是多模态 - CSDN博客
1 周前
内向的馒头  ·  多模态
1 周前
风流倜傥的烈马  ·  運用網頁比對與文本情感分析之資料隱碼攻擊漏洞自動偵測
6 天前
今天看啥   ·   Py中国   ·   codingpro   ·   小百科   ·   link之家   ·   卧龙AI搜索
删除内容请联系邮箱 2879853325@qq.com
小百科 - 百科知识指南
© 2024 ~ 沪ICP备11025650号