隨筆-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>

              但事實上不行,你會發現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 似水流年 閱讀(364) 評論(0)  編輯  收藏 所屬分類: Java
          主站蜘蛛池模板: 古蔺县| 大荔县| 手机| 双城市| 厦门市| 敖汉旗| 久治县| 逊克县| 天峨县| 河津市| 称多县| 南投县| 宣恩县| 山阳县| 平定县| 廉江市| 原阳县| 博乐市| 安福县| 通化县| 漳浦县| 林州市| 隆昌县| 宜宾县| 田东县| 曲麻莱县| 苍溪县| 县级市| 蓝田县| 福泉市| 驻马店市| 承德市| 民和| 潢川县| 竹北市| 民县| 崇州市| 大渡口区| 寿宁县| 吴堡县| 江山市|