关于在javabean判断虚拟路径和获取物理路径

。。。。。。。

    private Vector m_vector = new Vector();
   
    protected ServletContext m_application;
   
    private boolean m_denyPhysicalPath;

。。。。。。

    private boolean isVirtual(String pathName) //判断是否是虚拟路径
    {
        if(m_application.getRealPath(pathName) != null)
        {
            java.io.File virtualFile = new java.io.File(m_application.getRealPath(pathName));
            return virtualFile.exists();
        }
        else
        {
            return false;
        }
    }

    private String getPhysicalPath(String filePathName, int option)
     throws IOException
    {
        String path = new String();
        String fileName = new String();
        String fileSeparator = new String();
        boolean isPhysical = false;
        fileSeparator=System.getProperty("file.separator");
/*        if(filePathName == null)
        {
            throw new IllegalArgumentException("There is no specified destination file (1140).");
        }
        if(filePathName.equals(""))
        {
            throw new IllegalArgumentException("There is no specified destination file (1140).");
        }
//*/
               if(filePathName.lastIndexOf("//") >= 0)
        {
            path = filePathName.substring(0, filePathName.lastIndexOf("//"));
            fileName = filePathName.substring(filePathName.lastIndexOf("//") + 1);
        }
        if(filePathName.lastIndexOf("/") >= 0)
        {
            path = filePathName.substring(0, filePathName.lastIndexOf("/"));
            fileName = filePathName.substring(filePathName.lastIndexOf("/") + 1);
        }
        path = path.length() != 0 ? path : "/";
        java.io.File physicalPath = new java.io.File(path);
        if(physicalPath.exists())
        {
            isPhysical = true;
        }
        if(option == 0)
        {
            if(isVirtual(path))
            {
                path = m_application.getRealPath(path);
                if(path.endsWith(fileSeparator))
                    path = path + fileName;
                else
                    path = String.valueOf((new StringBuffer(String.valueOf(path))).append(fileSeparator).append(fileName));
                return path;
            }
            if(isPhysical)
            {
                if(m_denyPhysicalPath)
                    throw new IllegalArgumentException("Physical path is denied (1125).");
                else
                    return filePathName;
            }
            else
            {
                throw new IllegalArgumentException("This path does not exist (1135).");
            }
        }
        if(option == 1)
        {
            if(isVirtual(path))
            {
                path = m_application.getRealPath(path);
                if(path.endsWith(fileSeparator))
                    path = path + fileName;
                else
                    path = String.valueOf((new StringBuffer(String.valueOf(path))).append(fileSeparator).append(fileName));
                return path;
            }
            if(isPhysical)
                throw new IllegalArgumentException("The path is not a virtual path.");
            else
                throw new IllegalArgumentException("This path does not exist (1135).");
        }
        if(option == 2)
        {
            if(isPhysical)
                if(m_denyPhysicalPath)
                    throw new IllegalArgumentException("Physical path is denied (1125).");
                else
                    return filePathName;
            if(isVirtual(path))
                throw new IllegalArgumentException("The path is not a physical path.");
            else
                throw new IllegalArgumentException("This path does not exist (1135).");
        }
        else
        {
            return null;
        }
    }

。。。。。。。。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值