国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

用Java程序獲取絕對路徑

2019-11-18 13:37:32
字體:
供稿:網(wǎng)友

  前一段做個(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 下,用下面的程序似乎可以刪掉:

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

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

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

path_test.java的代碼如下:
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("/"));

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 广平县| 揭阳市| 隆昌县| 满洲里市| 大渡口区| 辽宁省| 蒙山县| 上犹县| 正镶白旗| 焦作市| 天全县| 安远县| 义马市| 桂东县| 白水县| 南投市| 全州县| 隆昌县| 睢宁县| 申扎县| 民权县| 峨边| 安陆市| 安乡县| 抚顺县| 武陟县| 巧家县| 铁岭县| 崇阳县| 宜川县| 施甸县| 新闻| 内乡县| 遂宁市| 汝州市| 黄梅县| 阿坝| 蒙城县| 福海县| 平和县| 德阳市|