一.JavaWeb中图片路径的问题
1.可以使用相对路径
eg. 使用"."以及".."来访问不同的路径
(1)路径
(2)代码
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<img src="./img/王珞丹.jpg">
</body>
</html>
(3)其他事项
..访问与.类似
2.使用绝对路径
(1)路径
桌面
(2)代码
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 <img src="file:///Users/amos/Desktop/王珞丹.jpg"> 11 </body> 12 </html>
(3)其他事项
不能使用request.getContextPath()来对其访问,下一点会讲有关路径的问题。如果要用需要使用
String t=Thread.currentThread().getContextClassLoader().getResource("").getPath(); int num=t.indexOf(".metadata"); String path="file:////"+t.substring(1,num)+"ForExam/WebContent/img/王珞丹.jpg";
来访问。
二.JavaWeb中文件路径的问题
转载:http://blog.csdn.net/znb769525443/article/details/50731789
1.用Jsp获取
(1)获取文件的绝对路径
String file="文件";(例如:DBUtil.java)
String path=application.getRealPath(file);
结果:
/Volumes/三千小世界/学习文件/java开发/workplace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/ForExam/DBUtil.java
Tip:
这里的文件可以是任意文件,不仅仅局限于当前项目的文件。
(2)获取当前jsp页面的路径
String p2=request.getRequestURI();
结果:
/ForExam/Test/Test.jsp
(3)获取当前项目的路径
String p3=request.getContextPath();
结果:
/ForExam
(4)获取当前项目子目录下的路径,
String p4=request.getServletPath();
结果:
/Test/Test.jsp
Tip:
与(2)类似,但是不包括根目录
2.用Java类获取
(1)获取Eclipse的运行路径
String a1=System.getProperty("user.dir");
结果:
/Users/amos/eclipse/jee-oxygen/Eclipse.app/Contents/MacOS
Tip:
windows可能会有所不同
(2)获取当前的classpath路径
String a2=User.class.getResource("").toString();
String a3=DBUtil.class.getResource("/").toString();
String a4=DBUtil.class.getClassLoader().getResource("").toString();
String t=Thread.currentThread().getContextClassLoader().getResource("").getPath();
打印出来分别是:
file:/Volumes/三千小世界/学习文件/java开发/workplace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/ForExam/WEB-INF/classes/com/jaovo/school/model/
file:/Volumes/三千小世界/学习文件/java开发/workplace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/ForExam/WEB-INF/classes/
file:/Volumes/三千小世界/学习文件/java开发/workplace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/ForExam/WEB-INF/classes/
/Volumes/三千小世界/学习文件/java开发/workplace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/ForExam/WEB-INF/classes/
Tip:
没有看懂!!!!
(3)获取文件的绝对路径
如果要获取WebContent目录下的文件绝对路径怎么办?可以用下面的方法
String t=Thread.currentThread().getContextClassLoader().getResource("").getPath();
int num=t.indexOf(".metadata");
String path=t.substring(1,num)+"项目名/WebContent/文件";
结果是:
Volumes/三千小世界/学习文件/java开发/workplace/项目名/WebContent/文件
Tip:
*其实就是(1)中的参数,去掉metadata后面的部分
*windows的/有所不同
3.用servlet获取
(1)获取项目的绝对路径
request.getSession().getServletContext().getRealPath("")
结果:
/Volumes/三千小世界/学习文件/java开发/workplace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/ForExam/
(2)获取浏览器地址
request.getRequestURL()
结果:
http://localhost:8080/ForExam/Test/Test.jsp
(3)获取当前文件的绝对路径
request.getSession().getServletContext().getRealPath(request.getRequestURI())
结果:
/Volumes/三千小世界/学习文件/java开发/workplace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/ForExam/ForExam/Test/Test.jsp