小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

用Java程序獲取絕對路徑

 looline 2006-11-18
用Java程序獲取絕對路徑
        

前一段做個(gè)程序,遇到了這樣一個(gè)問題,想利用相對路徑刪掉一個(gè)文件(實(shí)際存在的),老是刪不掉. 真是急人呀,最后讓我費(fèi)了好大力氣才算把它解決掉,問題不防跟大家說說,萬一遇到這樣的問題,就不用再費(fèi)勁了!

情況是這樣的:我的Tomcat裝在了c盤,而我的虛擬目錄設(shè)在了E:/work下, 我在E:/work/test/image下有個(gè)圖片,test.gif 我想通過程序刪掉它,但他的絕對路徑不確定(為了考慮到程序以后的移植,絕對路徑是不確定的)。

假設(shè)del.jsp文件在e:/work/test 下,用下面的程序好像可以刪掉:

<CCID_NOBR>

<CCID_CODE><!--原始的del.jsp源文件-->
                        <%@ page contentType="text/html; charset=GBK" errorPage="" %>
                        <%request.setCharacterEncoding("GBK");%>
                        <%@ page language="java" import="java.sql.*" import="java.util.*" import ="java.text.*" import="java.io.*"%>
                        <html>
                        <head>
                        <meta http-equiv="Content-Type" content="text/html; charset=GBK">
                        <title>刪除成功頁面</title>
                        </head>
                        <body>
                        File f=new File("/image/",test.gif);
                        boolean a=f.delete();
                        out.print("a="+a);
                        </body>
                        </html></CCID_CODE>
</CCID_NOBR>

但事實(shí)上不行,你會(huì)發(fā)現(xiàn)a=false;

這就需要獲取其絕對路徑, 我們用java程序來做一個(gè)專門來獲取絕對路徑的javaBean(path_test.java)就可以了。

path_test.java的代碼如下:

<CCID_NOBR>
<CCID_CODE>package pathtest;
                        import java.io.*;
                        import javax.servlet.*;
                        import javax.servlet.jsp.PageContext;//導(dǎo)入PageContext類,不要忘了
                        public class path_test
                        {
                        protected ServletContext m_application;
                        private boolean m_denyPhysicalPath;
                        public path_test()
                        {
                        }
                        public final void initialize(PageContext pageContext)
                        throws ServletException
                        {
                        m_application = pageContext.getServletContext();
                        }
                        public 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;
                        }
                        }
                        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;
                        }
                        }
                        }</CCID_CODE>
</CCID_NOBR>

對path_test.java編譯后,得到包pathtest,里面有path_test.class類,

把整個(gè)包放到虛擬目錄的classes下,然后再把del.jsp文件改成如下程序,一切都OK了!

<CCID_NOBR>
<CCID_CODE><!--改后的del.jsp的源文件-->
                        <%@ page contentType="text/html; charset=GBK" errorPage="" %>
                        <%request.setCharacterEncoding("GBK");%>
                        <%@ page language="java" import="java.sql.*" import="java.util.*" import ="java.text.*" import="java.io.*"%>
                        <html>
                        <head>
                        <meta http-equiv="Content-Type" content="text/html; charset=GBK">
                        <title>刪除成功頁面</title>
                        </head>
                        <body>
                        <jsp:useBean id="pathtest" scope="page" class="pathtest.path_test" />
                        pathtest.initialize(pageContext);//初始化
                        String dir1=pathtest.getPhysicalPath("/test/image/",0);//傳參數(shù)
                        out.print(dir1);//輸出的是絕對路徑
                        File file=new File(dir1,"test.gif");//生成文件對象
                        boolean a=file.delete();
                        out.print("a="+a);
                        </body">
                        </html></CCID_CODE>
</CCID_NOBR>

此時(shí)a=true;表示刪除成功!

到此為止,問題全部搞定。

    本站是提供個(gè)人知識管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多