绝对路径泄漏 java_用Java程序获取绝对路径

情况是这样的:我的Tomcat装在了c盘,而我的虚拟目录设在了E:/work下, 我在E:/work/test/image下有个图片,test.gif 我想通过程序删掉它,但他的绝对路径不确定(为了考虑到程序以后的移植,绝对路径是不确定的)。

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

删除成功页面

File f=new File("/image/",test.gif);

boolean a=f.delete();

out.print("a="+a);

但事实上不行,你会发现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了!

删除成功页面

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);

此时a=true;表示删除成功!

到此为止,问题全部搞定。

posted on 2005-06-07 16:19 似水流年 阅读(272) 评论(0)  编辑  收藏 所属分类: Java

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值