① java怎樣獲取當前目錄路徑
很多朋友都想知道java如何獲取當前目錄路徑?下面就一起來了解一下吧~
1、利用System.getProperty()函數獲取當前路徑:
System.out.println(System.getProperty("user.dir"));//user.dir指定了當前的路徑
2、使用File提供的函數獲取當前路徑:
File directory = new File("");//設定為當前文件夾 try{ System.out.println(directory.getCanonicalPath());//獲取標準的路徑 System.out.println(directory.getAbsolutePath());//獲取絕對路徑 }catch(Exceptin e){} File.getCanonicalPath()和File.getAbsolutePath()大約只是對於new File(".")和new File("..")兩種路徑有所區別。 # 對於getCanonicalPath()函數,「."就表示當前的文件夾,而」..「則表示當前文件夾的上一級文件夾 # 對於getAbsolutePath()函數,則不管」.」、「..」,返回當前的路徑加上你在new File()時設定的路徑 # 至於getPath()函數,得到的只是你在new File()時設定的路徑 比如當前的路徑為 C:/test : File directory = new File("abc"); directory.getCanonicalPath(); //得到的是C:/test/abc directory.getAbsolutePath(); //得到的是C:/test/abc direcotry.getPath(); //得到的是abc File directory = new File("."); directory.getCanonicalPath(); //得到的是C:/test directory.getAbsolutePath(); //得到的是C:/test/. direcotry.getPath(); //得到的是. File directory = new File(".."); directory.getCanonicalPath(); //得到的是C:/ directory.getAbsolutePath(); //得到的是C:/test/.. direcotry.getPath(); //得到的是.. 另外:System.getProperty()中的字元串參數如下: System.getProperty()參數大全 # java.version Java Runtime Environment version # java.vendor Java Runtime Environment vendor # java.vendor.url Java vendor URL # java.home Java installation directory # java.vm.specification.version Java Virtual Machine specification version # java.vm.specification.vendor Java Virtual Machine specification vendor # java.vm.specification.name Java Virtual Machine specification name # java.vm.version Java Virtual Machine implementation version # java.vm.vendor Java Virtual Machine implementation vendor # java.vm.name Java Virtual Machine implementation name # java.specification.version Java Runtime Environment specification version # java.specification.vendor Java Runtime Environment specification vendor # java.specification.name Java Runtime Environment specification name # java.class.version Java class format version number # java.class.path Java class path # java.library.path List of paths to search when loading libraries # java.io.tmpdir Default temp file path # java.compiler Name of JIT compiler to use # java.ext.dirs Path of extension directory or directories # os.name Operating system name # os.arch Operating system architecture # os.version Operating system version # file.separator File separator ("/" on UNIX) # path.separator Path separator (":" on UNIX) # line.separator Line separator ("/n" on UNIX) # user.name User』s account name # user.home User』s home directory # user.dir User』s current working directory
JAVA中獲取路徑 關鍵字: 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
② JSP獲得根目錄路徑
使用以下方式,來如果獲自取不到,那麼說明你的伺服器原本就不在E:盤,那麼建議你使用絕對路徑來解決這個問題,直接定義路徑,而不要獲取路徑。 使用<%=request.getRealPath(".")%>即可得到根目錄,必須調用以下幾個包:<%@ page import="java.util.*" %>
<%@ page import="javax.servlet.http.*" %>
<%@ page import="javax.servlet.*" %>
<%@ page import="java.io.*" %>
③ JSP如何取得<input type="file"> 文件路徑
問題補充:我使用strFile = request.getParameter("file")只能取得文件名,會發生I/O異常,提示找不到文件路徑; <br />用new File(strFile).getAbsolutePath()得到了路徑,但是得到的路徑形式為:%Tomcat_dir%\bin\XX,XX為文件名 問題補充:<div class="quote_title">lyyf 寫道</div><div class="quote_div">1.用request.getParameter(String str) <br /><input type="file" name="fileName" <br />後台用String fileName=request.getParameter("fileName")獲取,試試 <br />2.直接獲取伺服器上的內容 <br /> FileItemFactory factory = new DiskFileItemFactory(); <br /> ServletFileUpload upload = new ServletFileUpload(factory); <br /> List<FileItem list = upload.parseRequest(request); <br />不知道對不對,希望對你有幫助 <br /> <br /> <br /> <br /></div> <br />String fileName=request.getParameter("fileName")這個我試過了只能得到文件名,瀏覽器我用IE9、firefox 4試過都不行;我僅僅是想獲得<input type="file" name="fileName"的路徑 問題補充:<div class="quote_title">maxm 寫道</div><div class="quote_div">1.是以post方式提交的的嗎 <br />2.有無 :enctype= "multipart/form-data " <br />3.可以用隱藏iframe的方式</div> <br /> <br /> <br />是post提交,如果有enctype= "multipart/form-data "的話,程序就出錯,運行起來沒反應,我在IE6試了下,可以取得路徑(不過此時我的程序能正常運行,但是有亂碼出現) 問題補充:<div class="quote_title">maxm 寫道</div><div class="quote_div">貼出異常信息</div> <br /> <br />Path is:F:\apache-tomcat-6.0.29\bin\log.txt(這是我使用new File(strFile).getAbsolutePath();取得的路徑) <br />javax.mail.MessagingException: IOException while sending message; <br /> nested exception is: <br /> java.io.FileNotFoundException: F:\apache-tomcat-6.0.29\bin\log.txt (系統找不到指定的文件。) <br /> at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:930) <br /> at service.Mail.startSend(Mail.java:460) <br /> at org.apache.jsp.send_jsp._jspService(send_jsp.java:178) <br /> at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) <br /> at javax.servlet.http.HttpServlet.service(HttpServlet.java:820) <br /> at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377) <br /> at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313) <br /> at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260) <br /> at javax.servlet.http.HttpServlet.service(HttpServlet.java:820) <br /> at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290) <br /> at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) <br /> at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233) <br /> at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) <br /> at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) <br /> at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) <br /> at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) <br /> at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298) <br /> at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:861) <br /> at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:579) <br /> at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1584) <br /> at java.lang.Thread.run(Unknown Source) <br />Caused by: java.io.FileNotFoundException: F:\apache-tomcat-6.0.29\bin\log.txt (系統找不到指定的文件。) <br /> at java.io.FileInputStream.open(Native Method) <br /> at java.io.FileInputStream.<init(Unknown Source) <br /> at javax.activation.FileDataSource.getInputStream(Unknown Source) <br /> at javax.activation.DataHandler.writeTo(Unknown Source) <br /> at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:1383) <br /> at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:852) <br /> at javax.mail.internet.MimeMultipart.writeTo(MimeMultipart.java:453) <br /> at com.sun.mail.handlers.multipart_mixed.writeTo(multipart_mixed.java:98) <br /> at javax.activation.ObjectDataContentHandler.writeTo(Unknown Source) <br /> at javax.activation.DataHandler.writeTo(Unknown Source) <br /> at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:1383) <br /> at javax.mail.internet.MimeMessage.writeTo(MimeMessage.java:1743) <br /> at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:888) <br /> ... 20 more <br />java.io.FileNotFoundException: F:\apache-tomcat-6.0.29\bin\log.txt (系統找不到指定的文件。) <br /> at java.io.FileInputStream.open(Native Method) <br /> at java.io.FileInputStream.<init(Unknown Source) <br /> at javax.activation.FileDataSource.getInputStream(Unknown Source) <br /> at javax.activation.DataHandler.writeTo(Unknown Source) <br /> at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:1383) <br /> at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:852) <br /> at javax.mail.internet.MimeMultipart.writeTo(MimeMultipart.java:453) <br /> at com.sun.mail.handlers.multipart_mixed.writeTo(multipart_mixed.java:98) <br /> at javax.activation.ObjectDataContentHandler.writeTo(Unknown Source) <br /> at javax.activation.DataHandler.writeTo(Unknown Source) <br /> at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:1383) <br /> at javax.mail.internet.MimeMessage.writeTo(MimeMessage.java:1743) <br /> at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:888) <br /> at service.Mail.startSend(Mail.java:460) <br /> at org.apache.jsp.send_jsp._jspService(send_jsp.java:178) <br /> at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) <br /> at javax.servlet.http.HttpServlet.service(HttpServlet.java:820) <br /> at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377) <br /> at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313) <br /> at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260) <br /> at javax.servlet.http.HttpServlet.service(HttpServlet.java:820) <br /> at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290) <br /> at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) <br /> at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233) <br /> at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) <br /> at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) <br /> at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) <br /> at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) <br /> at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298) <br /> at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:861) <br /> at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:579) <br /> at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1584) <br /> at java.lang.Thread.run(Unknown Source) <br />
④ jsp怎麼獲取當前頁面的url
當一個url過來時,如:http://localhost:8080/demo/hello.jsp,在hello.jsp頁面,我們可以這樣得到url:
代碼如下:
<%Stringbasepath
=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort();
StringPath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"+request.getContextPath()+"/";
Stringuri=request.getRequestURI();
uri=uri.substring(uri.lastIndexOf("/")+1);//獲得是最後的hello.jsp
%>
(上例中,Path路徑就是圖中的)
說明:
1.request.getContextPath() 返回站點的根目錄,如:『』/demo『』
2.request.getRealpath("/")得到的是實際的物理路徑,也就是你的項目所在伺服器中的路徑
3.request.getScheme() 等到的是協議名稱,默認是http
4.request.getServerName() 得到的是在伺服器的配置文件中配置的伺服器名稱 比如:localhost ..com 等等
5.request.getServerPort() 得到的是伺服器的配置文件中配置的埠號 比如 8080等等
OK,滿意的話請好評!O(∩_∩)O~
⑤ jsp中 input file選擇文件 怎麼得到文件的路徑啊 瀏覽器是ie8
<script type="text/javascript">
//FX獲取文件路徑方法
function readFileFirefox(fileBrowser) {
try {
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
}
catch (e) {
alert('無法訪問本地文件,由於瀏覽器安全設置。為了克服這一點,請按照下列步驟操作:(1)在地址欄輸入"about:config";(2) 右鍵點擊並選擇 New->Boolean; (3) 輸入"signed.applets.codebase_principal_support" (不含引號)作為一個新的首選項的名稱;(4) 點擊OK並試著重新載入文件');
return;
}
var fileName=fileBrowser.value; //這一步就能得到客戶端完整路徑。下面的是否判斷的太復雜,還有下面得到ie的也很復雜。
var file = Components.classes["@mozilla.org/file/local;1"]
.createInstance(Components.interfaces.nsILocalFile);
try {
// Back slashes for windows
file.initWithPath( fileName.replace(/\//g, "\\\\") );
}
catch(e) {
if (e.result!=Components.results.NS_ERROR_FILE_UNRECOGNIZED_PATH) throw e;
alert("File '" + fileName + "' cannot be loaded: relative paths are not allowed. Please provide an absolute path to this file.");
return;
}
if ( file.exists() == false ) {
alert("File '" + fileName + "' not found.");
return;
}
return file.path;
}
//根據不同瀏覽器獲取路徑
function getvl(){
//判斷瀏覽器
var Sys = {};
var ua = navigator.userAgent.toLowerCase();
var s;
(s = ua.match(/msie ([\d.]+)/)) ? Sys.ie = s[1] :
(s = ua.match(/firefox\/([\d.]+)/)) ? Sys.firefox = s[1] :
(s = ua.match(/chrome\/([\d.]+)/)) ? Sys.chrome = s[1] :
(s = ua.match(/opera.([\d.]+)/)) ? Sys.opera = s[1] :
(s = ua.match(/version\/([\d.]+).*safari/)) ? Sys.safari = s[1] : 0;
var file_url="";
if(Sys.ie<="6.0"){
//ie5.5,ie6.0
file_url = document.getElementById("file").value;
}else if(Sys.ie>="7.0"){
//ie7,ie8
var file = document.getElementById("file");
file.select();
file_url = document.selection.createRange().text;
}else if(Sys.firefox){
//fx
//file_url = document.getElementById("file").files[0].getAsDataURL();//獲取的路徑為FF識別的加密字元串
file_url = readFileFirefox(document.getElementById("file"));
}
//alert(file_url);
document.getElementById("text").innerHTML="獲取文件域完整路徑為:"+file_url;
}
</script>
<h1>JS獲取文件域完整路徑的方法,兼容不同瀏覽器</h1>
<div id="text" style="color:#f00;"></div>
<input type="file" id="file" />
<input name="" type="button" value="獲取" onClick="getvl();">
⑥ jsp怎麼實現文件夾選擇並獲取文件夾路徑
<input type="file"> 用value來獲取文件夾的 路徑。
⑦ jsp頁面內選擇文件夾並獲得路徑
document.getElementById(path).value =Folder;
改為
document.getElementById("path").value =Folder;
你的path應該加雙引號!
50分賺到手了,哈哈回~~答~
⑧ 如何獲取項目絕對路徑
用獲取、用Java類獲取或用servlet獲取項目絕對路徑。