
由于采用了 Access 数据库,数据库文件的绝对路径是获取数据库连接时的关键参数,而本例中又同时采用了 JSF 框架,JSF 页面调用 JavaBean 中的方法时与普通 JSP 页面不同,要想将 request 对象直接传递到 JavaBean 的相关方法中来获取站点路径是比较麻烦的。前后思索想到一个笨方法,可以直接在获取连接的方法中获得路径,无需从 JSF 页面获取参数。代码如下:
public static Connection getConnection() {// 获得当前 JSF 上下文环境
FacesContext context = FacesContextgetCurrentInstance();
// 获得 FacesContext 的 Application 对象
Application application = contextgetApplication();
// 获得 classes 目录的绝对路径
URL classesUrl = applicationgetClass()getResource("/");
// 获得数据库文件的绝对路径
String dbpath = classesUrltoString() + "//data/blogDatamdb";
// 截去 URL 前端文件访问协议 file:// 字符串
dbpath = dbpathsubstring(6);
String url = "jdbc:odbc:driver={Microsoft Access Driver (mdb)};DBQ="
+ dbpath;
Connection conn = null;
try {
ClassforName("sunjdbcodbcJdbcOdbcDriver");
conn = DriverManagergetConnection(url, "", "");
}catch(Exception ex) {
exprintStackTrace();
}
return conn;
}
比如你的网站在C盘下的WEB目录
绝对路径就是C:/WEB/indexhtml
相对路径就是网站根目录的路径 比如你的网站目录是WEB 里面有一个叫A的文件夹
相对路径就是/A/jpg
假设有一个项目名为:report_emp
requestgetContextPath()获得的是当前的项目名 /report_emp,
要想获得项目实际在磁盘中存贮路径可以使用requestgetSession()getServletContext()getRealPath("/");
本地资源管理器而言:
绝对路径:是从盘符开始的路径,磁盘上真正的路径的,例如:E:\帮助文档\Jquery\jqAPI;
相对路径:是从当前路径开始的路径,如当前路径为E:\帮助文档,则Jquery\jqAPI为相对路径;
就web站点而言:
假如我们在report_emp的admin/loginjsp中访问了report_emp/admin/images/logogif的
绝对路径:以Web 站点根目录为参考基础的目录路径;
在loginjsp中的src则为report_emp/admin/images/logogif
相对路径:以引用文件之网页所在位置为参考基础,而建立出的目录路径;
在loginjsp中的src则为/images/logogif
<input id="myfile" type="file" />
<br />
<img src="" alt="Image to be upload" />
<div id="info"></div>
<script type="text/javascript">
var dFile = documentgetElementById('myfile');
var dImg = documentgetElementsByTagName('img')[0];
var dInfo = documentgetElementById('info');
dFileonchange = function(){
if(!dFilevaluematch(/jpg|gif|png|bmp/i)){alert('File type must be: jpg, gif, bmp or png !');return;}
if(dFilefiles){
dImgsrc = dFilefiles[0]getAsDataURL();
alert(dImgsrc);
}else if(dFilevalueindexOf('\\') > -1 || dFilevalueindexOf('\/') > -1){
dImgsrc = dFilevalue;
alert(dImgsrc);
}
}
</script>
输出 完整路径。
如果你是单纯的要获取绝对路径,可以用1楼的方法如果你是要获取文件路径后对文件 *** 作,你可以用TextBox和FileUpload组合,把FileUpload的宽设为0后,这个组合看上去就仍然像一个FileUpload,然后在pageload里面写下面一行:
thisFileUpload1AttributesAdd("onchange",
"documentgetElementById('"
+
TextBox1ClientID
+
"')value
=
thisvalue");
这样TextBox的text属性值就是所选文件的绝对路径值
import javaioFile;
public class MainTest {
public static void main(String[] args) {
//获取是项目的绝对路径
Systemoutprintln(SystemgetProperty("userdir"));
Systemoutprintln(new File("")getAbsolutePath());
//获取到clsspath绝对路径
Systemoutprintln(MainTestclassgetResource("/"));
Systemoutprintln(ThreadcurrentThread()getContextClassLoader()getResource(""));
Systemoutprintln(TestclassgetClassLoader()getResource(""));
}
}
前提是,你项目要部署到D:\tool\apache-tomcat-6020\webapps下!
建议你根据具体需求选择适合的方法!
有问题再追问,good luck!
第一种:File directory = new File("");//参数为空String courseFile = directorygetCanonicalPath() ;Systemoutprintln(courseFile);结果:C:\Documents and Settings\Administrator\workspace\projectName获取当前类的所在工程路径;
第二种:File f = new File(thisgetClass()getResource("/")getPath());Systemoutprintln(f);结果:C:\Documents and Settings\Administrator\workspace\projectName\bin获取当前类的所在工程路径;如果不加“/”File f = new File(thisgetClass()getResource("")getPath());Systemoutprintln(f);结果:C:\Documents and Settings\Administrator\workspace\projectName\bin\com\test获取当前类的绝对路径;
第三种:URL xmlpath = thisgetClass()getClassLoader()getResource("selectedtxt");Systemoutprintln(xmlpath);结果:file:/C:/Documents and Settings/Administrator/workspace/projectName/bin/selectedtxt获取当前工程src目录下selectedtxt文件的路径
第四种:Systemoutprintln(SystemgetProperty("userdir"));结果:C:\Documents and Settings\Administrator\workspace\projectName获取当前工程路径
第五种:Systemoutprintln( SystemgetProperty("javaclasspath"));结果:C:\Documents and Settings\Administrator\workspace\projectName\bin获取当前工程路径
以上就是关于如何取得Bean的class的绝对路径全部的内容,包括:如何取得Bean的class的绝对路径、怎么获取网站的绝对路径、request.getContextPath()获取的谁的绝对路径什么叫绝对路径等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)