获取HttpSession缓存大小(Java代码)

来源:互联网 发布:win10安装软件 知乎 编辑:程序博客网 时间:2024/06/06 02:02
/**
     * 获取HttpSession缓存大小
     * @param session HttpSession
     * @param filePath 输入文件
     */
    public static void getSessionSize(HttpSession session, String filePath)
    {
        FileOutputStream fileOutputStream = null;
        ObjectOutputStream objectOutputStream = null;

        try
        {
            fileOutputStream = new FileOutputStream(filePath);
            objectOutputStream = new ObjectOutputStream(fileOutputStream);
            objectOutputStream.writeObject("session:");
            Enumeration<String> names = session.getAttributeNames();

            while (names.hasMoreElements())
            {
                objectOutputStream.writeObject(session.getAttribute(names.nextElement()));
            }

            objectOutputStream.flush();
        }
        catch (Exception e)
        {
            e.printStackTrace();

            try
            {
                if (null != objectOutputStream)
                {
                    objectOutputStream.close();
                }
            }
            catch (IOException e1)
            {
                System.out.println(e1.toString());
            }

            try
            {
                if (null != fileOutputStream)
                {
                    fileOutputStream.close();
                }
            }
            catch (IOException e1)
            {
                System.out.println(e1.toString());
            }
        }
        finally
        {
            if (null != objectOutputStream)
            {
                try
                {
                    objectOutputStream.close();
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }
            }
            
            if (null != fileOutputStream)
            {
                try
                {
                    fileOutputStream.close();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
        }
    }
0 0
原创粉丝点击