• 2004-06-19

    用Java程序获取绝对路径 - [计算机]

    版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明
    http://java511.yourblog.org/logs/205137.html

    在赛迪网上发表的一篇文章,不错的! http://tech.ccidnet.com/pub/article/c1078_a120987_p1.html 用Java程序获取绝对路径 作者:侯广新 发文时间:2004.06.15 前一段做个程序,遇到了这样一个问题,想利用相对路径删掉一个文件(实际存在的),老是删不掉. 真是急人呀,最后让我费了好大力气才算把它解决掉,问题不防跟大家说说,万一遇到这样的问题,就不用再费劲了! 情况是这样的:我的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.g

    收藏到:Del.icio.us




    Tag:

发表评论

您将收到博主的回复邮件
记住我