用Java程式擷取絕對路徑

來源:互聯網
上載者:User
前一段做個程式,遇到了這樣一個問題,想利用相對路徑刪掉一個檔案(實際存在的),老是刪不掉. 真是急人呀,最後讓我費了好大力氣才算把它解決掉,問題不防跟大家說說,萬一遇到這樣的問題,就不用再費勁了!

    情況是這樣的:我的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;表示刪除成功!

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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.