网站首页
JSP空间
动态资讯
开源项目
技术文档
资源下载
J2EE资源
客户论坛
在线支付
 
  技术文档>>JAVA>>新手入门>>基础入门>查看文档  
  用java程序获取绝对路径     
  文章作者:未知  文章来源:水木森林  
  查看:100次  录入:管理员--2007-11-17  
 
  前一段做个程序,遇到了这样一个问题,想利用相对路径删掉一个文件(实际存在的),老是删不掉. 真是急人呀,最后让我费了好大力气才算把它解决掉,问题不防跟大家说说,万一遇到这样的问题,就不用再费劲了!

情况是这样的:我的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;表示删除成功!

到此为止,问题全部搞定。
 
 
上一篇: 用java插件在浏览器中运行java2 applet    下一篇: 用java创建带图标和缩进的jcombobox
  相关文档
用rmi和corba进行分布式java编程 11-16
ejb 2.1中实现web service 11-16
java applet 入门(目录) 11-16
java 理论与实践: 用弱引用堵住内存泄漏 11-17
首个java恶意手机代码出现 11-17
caller 属性 11-16
学习j2se过程中的30个基本概念 11-16
java编程中的中的“文档”和“包” 11-17
java图形设计卷i awt 第一章绪论 11-16
gel:号称世界上最轻巧的javaide 11-17
java入门教程:数据类型 11-17
破除java神话 11-17
java api之实现(下) 11-16
java中properties类的使用 11-17
接口设计中的性能问题 11-17
resin+apache的安装运行jsp/servlet 11-17
java入门笔记4_访问控制和包 11-17
用java实现web服务器 http协议 11-17
加密网页破解大法 11-17
我的 scjp 1.4 考试心得 11-17
返回首页 | 关于我们 | J网章程 | JSP空间合租 | 客服中心 | 免责声明 | 常见问题 | 参观机房
本站主机空间代理至厦门市华众网络科技有限公司
《中华人民共和国增值电信业务经营许可证》
编号:闽B2-20050079
@2005-2008福建JSP技术网 版权所有 闽ICP备05000928号
技术电话:13616026886
邮箱:admin@fjjsp.com 站长QQ,点击这里给我发消息