您的位置:首页 > 理论基础 > 计算机网络

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

2014-07-29 11:21 281 查看
/**

     * 获取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();

                }

            }

        }

    }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: