在jsp和class文件中调用的相对路径不同。在jsp里,根目录是WebRoot 在class文件中,根目录是WebRoot/WEB-INF/classes 当然你也可以用System.getProperty("user.dir")获取你工程的绝对路径。
另:在Jsp,Servlet,Java中详细获得路径的方法!
1.jsp中取得路径:
以工程名为TEST为例:
(1)得到包含工程名的当前页面全路径:request.getRequestURI()
结果:/TEST/test.jsp
(2)得到工程名:request.getContextPath()
结果:/TEST
(3)得到当前页面所在目录下全名称:request.getServletPath()
结果:如果页面在jsp目录下 /TEST/jsp/test.jsp
(4)得到页面所在服务器的全路径:application.getRealPath("页面.jsp")
结果:D:\resin\webapps\TEST\test.jsp
(5)得到页面所在服务器的绝对路径:absPath=new java.io.File(application.getRealPath(request.getRequestURI())).getParent();
结果:D:\resin\webapps\TEST
2.在类中取得路径:
(1)类的绝对路径:Class.class.getClass().getResource("/").getPath()
结果:/D:/TEST/WebRoot/WEB-INF/classes/pack/
(2)得到工程的路径:System.getProperty("user.dir")
结果:D:\TEST
3.在Servlet中取得路径:
(1)得到工程目录:request.getSession().getServletContext().getRealPath("") 参数可具体到包名。
结果:E:\Tomcat\webapps\TEST
(2)得到IE地址栏地址:request.getRequestURL()
结果:http://localhost:8080/TEST/test
(3)得到相对地址:request.getRequestURI()
结果:/TEST/test
2011-01-04 11:40
另,Class类还有一个getResourceAsStream方法,记得以前有个项目要读取在同一个包内的一个xml,就用的这个。
1.如何获得当前文件路径
常用:
(1).Test.class.getResource("")
得到的是当前类FileTest.class文件的URI目录。不包括自己!
(2).Test.class.getResource("/")
得到的是当前的classpath的绝对URI路径。
(3).Thread.currentThread().getContextClassLoader().getResource("")
得到的也是当前ClassPath的绝对URI路径。
(4).Test.class.getClassLoader().getResource("")
得到的也是当前ClassPath的绝对URI路径。
(5).ClassLoader.getSystemResource("")
得到的也是当前ClassPath的绝对URI路径。
尽量不要使用相对于System.getProperty("user.dir")当前用户目录的相对路径,后面可以看出得出结果五花八门。
(6) new File("").getAbsolutePath()也可用。
2.Web服务器
(1).Tomcat
在类中输出System.getProperty("user.dir");显示的是%Tomcat_Home%/bin
(2).Resin
不是你的JSP放的相对路径,是JSP引擎执行这个JSP编译成SERVLET
的路径为根.比如用新建文件法测试File f = new File("a.htm");
这个a.htm在resin的安装目录下
(3).如何读文件
使用ServletContext.getResourceAsStream()就可以
(4).获得文件真实路径
String file_real_path=ServletContext.getRealPath("mypath/filename");
不建议使用request.getRealPath("/");
3.文件操作的类,不建议使用,可以使用commons io类
import java.io.*;
import java.net.*;
import java.util.*;
public class FileUtil {
private FileUtil() {
}
public static void touch(File file) {
long currentTime = System.currentTimeMillis();
if (!file.exists()) {
System.err.println("file not found:" + file.getName());
System.err.println("Create a new file:" + file.getName());
try {
if (file.createNewFile()) {
// System.out.println("Succeeded!");
}
else {
// System.err.println("Create file failed!");
}
}
catch (IOException e) {
// System.err.println("Create file failed!");
e.printStackTrace();
}
}
boolean result = file.setLastModified(currentTime);
if (!result) {
// System.err.println("touch failed: " + file.getName());
}
}
public static void touch(String fileName) {
File file = new File(fileName);
touch(file);
}
public static void touch(File[] files) {
for (int i = 0; i < files.length; i++) {
touch(files);
}
}
public static void touch(String[] fileNames) {
File[] files = new File[fileNames.length];
for (int i = 0; i < fileNames.length; i++) {
files = new File(fileNames);
}
touch(files);
}
public static boolean isFileExist(String fileName) {
return new File(fileName).isFile();
}
public static boolean makeDirectory(File file) {
File parent = file.getParentFile();
if (parent != null) {
return parent.mkdirs();
}
return false;
}
public static boolean makeDirectory(String fileName) {
File file = new File(fileName);
return makeDirectory(file);
}
public static boolean emptyDirectory(File directory) {
boolean result = false;
File[] entries = directory.listFiles();
for (int i = 0; i < entries.length; i++) {
if (!entries.delete()) {
result = false;
}
}
return true;
}
public static boolean emptyDirectory(String directoryName) {
File dir = new File(directoryName);
return emptyDirectory(dir);
}
public static boolean deleteDirectory(String dirName) {
return deleteDirectory(new File(dirName));
}
public static boolean deleteDirectory(File dir) {
if ( (dir == null) || !dir.isDirectory()) {
throw new IllegalArgumentException("Argument " + dir +
" is not a directory. ");
}
File[] entries = dir.listFiles();
int sz = entries.length;
for (int i = 0; i < sz; i++) {
if (entries.isDirectory()) {
if (!deleteDirectory(entries)) {
return false;
}
}
else {
if (!entries.delete()) {
return false;
}
}
}
if (!dir.delete()) {
return false;
}
return true;
}
public static URL getURL(File file) throws MalformedURLException {
String fileURL = "file:/" + file.getAbsolutePath();
URL url = new URL(fileURL);
return url;
}
public static String getFileName(String filePath) {
File file = new File(filePath);
return file.getName();
}
public static String getFilePath(String fileName) {
File file = new File(fileName);
return file.getAbsolutePath();
}
public static String toUNIXpath(String filePath) {
return filePath.replace('\\', '/');
}
public static String getUNIXfilePath(String fileName) {
File file = new File(fileName);
return toUNIXpath(file.getAbsolutePath());
}
public static String getTypePart(String fileName) {
int point = fileName.lastIndexOf('.');
int length = fileName.length();
if (point == -1 || point == length - 1) {
return "";
}
else {
return fileName.substring(point + 1, length);
}
}
public static String getFileType(File file) {
return getTypePart(file.getName());
}
public static String getNamePart(String fileName) {
int point = getPathLsatIndex(fileName);
int length = fileName.length();
if (point == -1) {
return fileName;
}
else if (point == length - 1) {
int secondPoint = getPathLsatIndex(fileName, point - 1);
if (secondPoint == -1) {
if (length == 1) {
return fileName;
}
else {
return fileName.substring(0, point);
}
}
else {
return fileName.substring(secondPoint + 1, point);
}
}
else {
return fileName.substring(point + 1);
}
}
public static String getPathPart(String fileName) {
int point = getPathLsatIndex(fileName);
int length = fileName.length();
if (point == -1) {
return "";
}
else if (point == length - 1) {
int secondPoint = getPathLsatIndex(fileName, point - 1);
if (secondPoint == -1) {
return "";
}
else {
return fileName.substring(0, secondPoint);
}
}
else {
return fileName.substring(0, point);
}
}
public static int getPathIndex(String fileName) {
int point = fileName.indexOf('/');
if (point == -1) {
point = fileName.indexOf('\\');
}
return point;
}
public static int getPathIndex(String fileName, int fromIndex) {
int point = fileName.indexOf('/', fromIndex);
if (point == -1) {
point = fileName.indexOf('\\', fromIndex);
}
return point;
}
public static int getPathLsatIndex(String fileName) {
int point = fileName.lastIndexOf('/');
if (point == -1) {
point = fileName.lastIndexOf('\\');
}
return point;
}
public static int getPathLsatIndex(String fileName, int fromIndex) {
int point = fileName.lastIndexOf('/', fromIndex);
if (point == -1) {
point = fileName.lastIndexOf('\\', fromIndex);
}
return point;
}
public static String trimType(String filename) {
int index = filename.lastIndexOf(".");
if (index != -1) {
return filename.substring(0, index);
}
else {
return filename;
}
}
public static String getSubpath(String pathName,String fileName) {
int index = fileName.indexOf(pathName);
if (index != -1) {
return fileName.substring(index + pathName.length() + 1);
}
else {
return fileName;
}
}
}
4.遗留问题
目前new FileInputStream()只会使用绝对路径,相对没用过,因为要相对于web服务器地址,比较麻烦
还不如写个配置文件来的快哪
5.按Java文件类型分类读取配置文件
配 置文件是应用系统中不可缺少的,可以增加程序的灵活性。java.util.Properties是从jdk1.2就有的类,一直到现在都支持load ()方法,jdk1.4以后save(output,string) ->store(output,string)。如果只是单纯的读,根本不存在烦恼的问题。web层可以通过 Thread.currentThread().getContextClassLoader().
getResourceAsStream("xx.properties") 获取;Application可以通过new FileInputStream("xx.properties");直接在classes一级获取。关键是有时我们需要通过web修改配置文件,我们不 能将路径写死了。经过测试觉得有以下心得:
1.servlet中读写。如果运用Struts 或者Servlet可以直接在初始化参数中配置,调用时根据servletcontext的getRealPath("/")获取真实路径,再根据 String file = this.servlet.getInitParameter("abc");获取相对的WEB-INF的相对路径。
例:
InputStream input = Thread.currentThread().getContextClassLoader().
getResourceAsStream("abc.properties");
Properties prop = new Properties();
prop.load(input);
input.close();
OutputStream out = new FileOutputStream(path);
prop.setProperty("abc", “test");
prop.store(out, “–test–");
out.close();
2.直接在jsp中操作,通过jsp内置对象获取可操作的绝对地址。
例:
// jsp页面
String path = pageContext.getServletContext().getRealPath("/");
String realPath = path+"/WEB-INF/classes/abc.properties";
//java 程序
InputStream in = getClass().getClassLoader().getResourceAsStream("abc.properties"); // abc.properties放在webroot/WEB-INF/classes/目录下
prop.load(in);
in.close();
OutputStream out = new FileOutputStream(path); // path为通过页面传入的路径
prop.setProperty("abc", “abcccccc");
prop.store(out, “–test–");
out.close();
3.只通过Java程序操作资源文件
InputStream in = new FileInputStream("abc.properties"); // 放在classes同级
OutputStream out = new FileOutputStream("abc.properties");
分享到:
相关推荐
如何在java 编程中获取当前的路径
1、利用System.getProperty()函数获取当前路径: System.out.println(System.getProperty(user.dir));//user.dir指定了当前的路径 2、使用File提供的函数获取当前路径: File directory = new File(&#...
JSP中获得当前应用的相对路径和绝对路径 根目录所对应的绝对路径、 文件的绝对路径 、 当前web应用的绝对路径、 取得请求文件的上层目录……
利用System.getProperty()函数获取当前路径: System.out.println(System.getProperty("user.dir"));//user.dir指定了当前的路径
经常遇到路径问题 这些也是我从网上找到的 大家分享下
基本概念的理解绝对路径:绝对路径就是你的主页上的文件或目录在硬盘上真正的路径,(URL和物理路径)例如:C:xyz est.txt 代表了test.txt文件的绝对路径。http://www.sun.com/index.htm也代表了一个URL绝对路径。相对...
主要介绍了Java中获取当前路径的几种方法总结的相关资料,需要的朋友可以参考下
windows linux 下,获取java项目绝对路径的方法,struts2设置了struts.multipart.saveDir后会在根目录建立文件夹,这样会涉及linux下的权限问题
在Java程序中获取当前运行程序的路径在Java程序中获取当前运行程序的路径在Java程序中获取当前运行程序的路径在Java程序中获取当前运行程序的路径
开发web工程时经常要获取工程的根目录,自己用Java实现的获取Tomcat下war包部署的Web工程根目录路径的方法,主要利用web工程默认的目录结构,此外也可以指定工程名称获取工程目录的绝对路径
主要介绍了java如何获取当前路径下的所有xml文档,需要的朋友可以参考下
主要介绍了java获取系统路径字体、得到某个目录下的所有文件名、获取当前路径,需要的朋友可以参考下
获得当前项目的根路径,js写出的公共方法,可在jsp中直接调用
功能:获取java进程pid 进程可根据该pid调用exec自杀 注意事项:不能改变PidTool.java所在的包,否则需要重行生成dll。 编译命令中 "-PidTool.dll" 改为 "-FePidTool.dll",我整漏了! 使用方法: 1.javac PidTool...
主要为大家详细介绍了java获取各种路径的基本方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
java实现 获取指定路径下 指定格式 的所有文件
JAVA获取FTP文件列表,功能齐全。包括:链接到服务器,上传文件到FTP服务器,从FTP服务器上下载文件并返回下载文件长度,返回FTP目录下的文件列表,删除FTP上的文件及目录,关闭链接。
在本篇文章里小编给大家整理的是一篇关于linux中java获取路径的实例代码以及相关知识点,有兴趣的朋友们可以学习参考下。
Java 开发中我们经常要获取文件的路径,比如读取配置文件等等。今天我们就关于文件的路径和如何读取文件简单地探讨一下。 2. 文件的路径 文件的路径通常有 相对路径 与 绝对路径。 2.1 相对路径 以当前文件为基准...
从HttpServletRequest获取各种路径总结.docx从HttpServletRequest获取各种路径总结.docx