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

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

              假設(shè)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>刪除成功頁(yè)面</title>
          </head>
          <body>
          File f=new File("/image/",test.gif);
          boolean a=f.delete();
          out.print("a="+a);
          </body>
          </html>

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

              這就需要獲取其絕對(duì)路徑, 我們用java程序來(lái)做一個(gè)專(zhuān)門(mén)來(lái)獲取絕對(duì)路徑的javaBean(path_test.java)就可以了。

          path_test.java的代碼如下:
          package pathtest;
          import java.io.*;
          import javax.servlet.*;
          import javax.servlet.jsp.PageContext;//導(dǎo)入PageContext類(lèi),不要忘了
          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;
          }
          }

          }

              對(duì)path_test.java編譯后,得到包pathtest,里面有path_test.class類(lèi),

              把整個(gè)包放到虛擬目錄的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>刪除成功頁(yè)面</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);//輸出的是絕對(duì)路徑
          File file=new File(dir1,"test.gif");//生成文件對(duì)象
          boolean a=file.delete();
          out.print("a="+a);
          </body">
          </html>

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

              到此為止,問(wèn)題全部搞定。

          posted on 2005-06-07 16:19 似水流年 閱讀(361) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): Java
          主站蜘蛛池模板: 南安市| 奉贤区| 万安县| 疏附县| 霍城县| 乐清市| 祥云县| 靖边县| 凤城市| 通州区| 黎平县| 广宁县| 岳阳市| 临颍县| 迁安市| 定西市| 海门市| 岗巴县| 莱阳市| 新津县| 虞城县| 张掖市| 西丰县| 利川市| 湟中县| 保德县| 安化县| 北流市| 碌曲县| 汉沽区| 甘南县| 那曲县| 客服| 大城县| 昔阳县| 斗六市| 文山县| 兴文县| 绥滨县| 中超| 平塘县|