<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆-26  評論-13  文章-46  trackbacks-0
    前一段做個程序,遇到了這樣一個問題,想利用相對路徑刪掉一個文件(實際存在的),老是刪不掉. 真是急人呀,最后讓我費了好大力氣才算把它解決掉,問題不防跟大家說說,萬一遇到這樣的問題,就不用再費勁了!

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

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

    <!--原始的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>

        但事實上不行,你會發(fā)現a=false;

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

    path_test.java的代碼如下:
    package pathtest;
    import java.io.*;
    import javax.servlet.*;
    import javax.servlet.jsp.PageContext;//導入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;
    }
    }

    }

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

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

    <!--改后的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);//傳參數
    out.print(dir1);//輸出的是絕對路徑
    File file=new File(dir1,"test.gif");//生成文件對象
    boolean a=file.delete();
    out.print("a="+a);
    </body">
    </html>

        此時a=true;表示刪除成功!

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

    posted on 2005-06-07 16:19 似水流年 閱讀(358) 評論(0)  編輯  收藏 所屬分類: Java
    主站蜘蛛池模板: **一级一级毛片免费观看| 两个人看的www免费| 色影音免费色资源| 特黄特色大片免费| 卡1卡2卡3卡4卡5免费视频| 77777亚洲午夜久久多喷| 99re热免费精品视频观看| 国产成人精品日本亚洲专区6| 69xx免费观看视频| 亚洲乱码卡一卡二卡三| 韩国免费一级成人毛片| 亚洲人成网站日本片| 免费被黄网站在观看| 羞羞漫画在线成人漫画阅读免费| 在线a亚洲v天堂网2018| GOGOGO高清免费看韩国| 亚洲AV中文无码字幕色三| 久久久99精品免费观看| 亚洲精品国产福利片| 妞干网免费视频观看| 日韩毛片一区视频免费| 亚洲人成人无码网www电影首页| 久久国产乱子精品免费女| 亚洲性猛交xx乱| 国产午夜影视大全免费观看| www成人免费观看网站| 亚洲精品无码久久久影院相关影片| 国产精品亚洲一区二区三区久久| 全黄a免费一级毛片人人爱| 国产一级在线免费观看| 亚洲美女视频免费| 大陆一级毛片免费视频观看 | 精品无码国产污污污免费网站| 亚洲国产日韩在线人成下载 | 国产一卡2卡3卡4卡2021免费观看 国产一卡2卡3卡4卡无卡免费视频 | 亚洲中文字幕无码一久久区| 久久久国产精品无码免费专区| 精品亚洲成A人无码成A在线观看| 国产精品久免费的黄网站| 久久免费国产精品一区二区| 亚洲欧美国产精品专区久久|