如何获得Web站点的路径

onlie 2005-08-11 10:29:12
我在做jsp站点,很多功能采用了java类来实现,为了使站点易于移值,将DB连接字符串写到一个文本文件里,然后在再在类里读这个文件,但这时问题就出来了,如何在java类中获得该文件的路径?
谢!
...全文
118 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
ningshuihan 2005-08-12
  • 打赏
  • 举报
回复
保存为xxx.properties文件,放在类文件根目录下(Eclipse下为src文件夹)
再用ResourceBundle类来操作
onray 2005-08-11
  • 打赏
  • 举报
回复
写成property档 然后用ResouseBoundle获得
liuxiancan123 2005-08-11
  • 打赏
  • 举报
回复
可以通过获取该文件所在文件夹的绝对路径实现
代码:

import java.io.*;
import javax.servlet.*;
import javax.servlet.jsp.PageContext;//导入PageContext类,不要忘了
public class PhysicalPath
{
protected ServletContext m_application;
private boolean m_denyPhysicalPath;
public PhysicalPath()
{

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

该文件的路径获取:
//获取txt文件所在绝对路径
PhysicalPath physicalPath=new PhysicalPath();
physicalPath.initialize(pageContext);//初始化
String dir1=physicalPath.getPhysicalPath("/TXT文件夹名/",0);//传参数
String filepath=dir1+fileName;//文件的路径

81,090

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧